2

This is simple one i am using the following insert query

mysql_query(insert into table1 set saltval = 'Y'Z' where uid ='1');

but i does not work becaues the value for the field saltval is Y'Z . my question is how to considered this value is as a string .

4 Answers 4

5

You need to escape any single quotes with a backslash.

mysql_query("insert into table1 set saltval = 'Y\'Z' where uid ='1'");

However your SQL is invalid as well... Did you mean to do an update? Insert statements don't have a where.

As mentioned in other answers, if the input is from a user then you should use mysql_real_escape_string()
http://www.php.net/manual/en/function.mysql-real-escape-string.php

Sign up to request clarification or add additional context in comments.

Comments

2
$string = mysql_real_escape_string("Y'Z");
mysql_query("insert into table1 set saltval = '{$string}' where uid ='1'");

Comments

2

Always use mysql_real_escape_string() function for this if values come from user input

$query="insert into table1 set saltval = '".mysql_real_escape_string($InputVal)."' where uid ='1'";

See http://php.net/manual/en/function.mysql-real-escape-string.php

Comments

1

You have to add a backslash to certain characters to make your string fit into SQL syntax rules.
Assuming you're creating your query dynamically, PHP has special escaping function for this and you should use it for the every quoted string in the query, no exceptions.
So, write your code like this:

$salt = "Y'Z";
$id   = 1;

$salt = mysql_real_escape_string($salt);
$id   = mysql_real_escape_string($id);

$sql = "update table1 set saltval = '$salt' where uid ='$id'";

mysql_query($sql) or trigger_error(mysql_error()." ".$sql);

to make it safe and fault-tolerant

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.