简体   繁体   中英

Unknown column in where clause

This script is supposed to retrieve the CustomerID for the Customer_First_Name and Customer_Last_Name that has been entered into a form.

$query  = "SELECT CustomerID FROM customer WHERE Customer_First_Name = `.$db_customer_first_name.` AND Customer_Last_Name = `.$db_customer_last_name.`";
$result = mysql_query($query)
or die(mysql_error());
echo $result;
echo $query;

when the script runs I get this error:

Unknown column '.Christopher.' in 'where clause'

the query is never printed on the screen.

any help is appreciated.

您的报价不好用'而不是'

使用'代替remove`

$query  = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '".$db_customer_first_name."' AND Customer_Last_Name = '".$db_customer_last_name."'";

You have to use single quotes for strings. Try again:

$query  = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '".$db_customer_first_name."' AND Customer_Last_Name = '".$db_customer_last_name."'";

This should work:

$query  = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '$db_customer_first_name' AND Customer_Last_Name = '$db_customer_last_name'";

You need to use normal single quotes for values. Also you don't need to break the string if you're using double quotes - variables are detected within the string.

Make sure you're also correctly escaping your strings for mysql to prevent injection.

Better still, look at moving to mysqli and using prepared statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM