Skip to main content
Inspiring
September 20, 2011
Answered

Inserting special character like apostrophes with PHP/Mysql

  • September 20, 2011
  • 2 replies
  • 3955 views

Hi friends,

I have a problem with php/mysql. I have created a form inton a php page with dreamweaver and when i try to fill the form with a string containing apostrophe ( ' ), i have a error. It seems that the insertion is not possible. Could you have any idea for that. I give you the php dreamweaver code and thank you for your help.

<?php require_once('Connections/Mainconnect.php'); ?>

<?php require_once('Zend/Date.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

if ($theValue=="")

{

    $theValue="NULL";

}

else

{

    $zendDate=new Zend_Date($theValue,"dd/MM/yyyy");

    $theValue="'".$zendDate->toString("yyyy-MM-dd")."'";

}

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

  $updateSQL = sprintf("UPDATE pers_soc_reg SET field1=%s, field2=%s,

                       GetSQLValueString($_POST['field1'], "text"),

                       GetSQLValueString($_POST['field2'], "double"));

This topic has been closed for replies.
Correct answer lemzo1234

Hello Rob,

That's the message that i have after entering the word " l'amine "

The text after 'becomes' should be escaped (\')

l'amine becomes


Hello Friends,

I have solved the problem .

The problem was an free extension to dreamweaver called "Check new elements" from Felixone. After removing it, all forms work without problem.

Thank you all.

2 replies

Rob Hecker2
Legend
September 20, 2011

Apostrophes and quotations must be "escaped" before submission to the database. You can use mysql_real_escape_string() for this.

Or you can use entities (&#39;)

Or you can use PDO with bound variables

lemzo1234Author
Inspiring
September 20, 2011

My problem is HOW to put that in the code above ??? I have tried many solution but no result.

lemzo1234Author
Inspiring
September 20, 2011

Do you have any idea ?

Community Expert
September 20, 2011

What is the error you are receiving?

lemzo1234Author
Inspiring
September 20, 2011

The error is in French. It means that there is a mistake near the cote. For example, in field 1 of the form when i put  "L'amine" , It tells me there is an error near "amine".