简体   繁体   中英

How to use php var as same as column name in php mysql?

How to use php var as same as column name in php mysql ?

When access to this site

www.example.com/test.php?column=member_no&id=5

I will get $_GET['column'] = member_no and $_GET['id'] = 5

Then when i test below code why data not update ?

$sql = "UPDATE table SET '".mysql_real_escape_string($_GET['column'])."' = '0' WHERE id = '".mysql_real_escape_string($_GET['id'])."' ";
$dbQuery = mysql_query($sql);  

Use of Mysql Field name in PHP has its own convention, you can't use ' for a filed name, You need to use ` or left blank.

$sql = "UPDATE table SET `".mysql_real_escape_string($_GET['column'])."` = '0' WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
$dbQuery = mysql_query($sql);  

Note: mysql_* is depreciated in upper level PHP version and removed from PHP 7.*, So please avoid use them and start with mysqli_* or PDO.

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