简体   繁体   中英

Syntax on this MySQL query might be wrong

Is there something wrong with the syntax on this MySQL query?

Thanks in advance,

John

$ttquery = sprintf("Update login SET ".$row['ttemail']." = '1' WHERE username = ".$row['username'].""); 

EDIT: Okay, per Pekka's request, I echoed out the actual query value, and that gave me some ideas. Now I'm using this:

$ttquery = "Update login SET ttemail = 1 WHERE username = ".$row['username']."";

and I get this error: Unknown column 'admin' in 'where clause' . "admin" is the first username that meets the condition I want to run this query for... it's not the name of a field. Any ideas on why I'm getting the error?

EDIT: Here is the MySQL echoed MySQL query if that helps:

Update login SET ttemail = 1 WHERE username = admin

You probably need single quotes around username

$ttquery = "Update login SET ".$row['ttemail']." = '1' WHERE username = '".$row['username']."'";

If you're using sprintf , you would have:

$ttquery = sprintf("Update login SET %1$s = '1' WHERE username = '%2$s'", $row['ttemail'],$row['username']);
Update login SET ttemail = 1 WHERE username = admin

In SQL, strings are surrounded by single quotes and table/column names are unquoted. You need to fix your PHP code so you generate this:

Update login SET ttemail = 1 WHERE username = 'admin'

Try to make sure you understand basic SQL before banging your head against PHP ;-)

try this

$ttquery = sprintf("Update login SET ".$row['ttemail']." = '1' WHERE username = '" . $row['username'] ."'"

ie, username='[your value]'

这应该工作:

$ttquery = "Update login SET ".$row['ttemail']." = '1' WHERE username = '".$row['username']."'";

man, be careful about sql injections. Also, why call sprintf() if you dont actually use it?

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