Jump to content

Double Quotes


katriar

Recommended Posts

Hi,

 

I am very new in PHP. I am trying to make a page which takes input from user and a php file will parse the same. In sql query there are two parameter one is of int data type and other one is varchar. It takes the correct input from the 1st page but the value for varchar field is not coming in double or single quotes [" ",' '] thats why mysql does not parse the data. My sql query is as below.

 

$query= "select * from person where emp_id=" . $_POST['user'] . " and description=" . $_POST['user1'] . "";

 

Please suggest,

Link to comment
Share on other sites

Although looking up mysql_real_escape_string() is a good start, it's not the solution here.

 

Just wrap single quotes around your strings:

 

$query= "select * from person where emp_id='" . $_POST['user'] . "' and description='" . $_POST['user1'] . "'";

Link to comment
Share on other sites

If you need to use double quotes inside a string you need to escape them by using a backslash...

 

$string = "This is \"how to escape characters\"";
// alternatively just use single quotes
$string = "This is 'how to not need to escape characters'";

Also you really should pass any value input by the user through mysql_real_escape_string.

 

$query= "SELECT * FROM person WHERE emp_id='" . mysql_real_escape_string($_POST['user']) . "' AND description='" . mysql_real_escape_string($_POST['user1']) . "'";

Link to comment
Share on other sites

You can use this function

 

 

function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) 
{
	$value = stripslashes($value);
}
// Quote if not a number or a numeric string
if (!is_numeric($value)) 
{
	$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.