Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql query does not get updated due to apostrophe sign

$url = "example.com";
$data = json_decode($raw);
    $pname=$data->name;
$sql="UPDATE `client` SET pname='$pname' WHERE url='$url'";
    $query=mysql_query($sql,$link)or die(mysql_error());

When the json data is decoded, the value in variable $pname goes in client table. If there is an apostrophe sign (') in name then it throws an error. What changes can I make in the variable to send the name to database table?

example: Jerry get updated with no issues D'Cunha does not get updated as it has the apostrophe sign. The query becomes

"UPDATE `client` SET pname='D'Cunha' WHERE url='example.com'"

I found some articles but that does not say about how to find the apostrophe sign and change the variable value

like image 933
Mumbai CabinCrew Avatar asked Dec 10 '25 05:12

Mumbai CabinCrew


1 Answers

use mysql_escape_string()

$sql="UPDATE `client` SET pname='".mysql_escape_string($pname)."' WHERE url='$url'";

and learn mysqli or PDO as mysql is deprciated and soon going to be drop

like image 102
arif_suhail_123 Avatar answered Dec 12 '25 18:12

arif_suhail_123