简体   繁体   中英

The best Mysql query

I'm sending form data to db with UPDATE query:

mysql_query("UPDATE users SET price = '100-200' WHERE login = '$login'");
mysql_query("UPDATE users SET city = '$city' WHERE login = '$login'");

My question is: how to rebuild it to have query which writes data in db, but do not remove older posts.

For example: If user enters data 'price' and 'city', and after this, he wants to change only 'city', script with update will cancel 'price' and leave blank field in db.

How to make it to update (like in example) only city, but to leave price as it was before (100-200). Is there a proper query for this?

You will want to do a check for NULL or empty variables before running the SQL Statements. Something like this:

if(!empty($price))
{
    mysql_query("UPDATE `users` SET `price` = '".$price."' WHERE `login` = '".$login."';");
}
if(!empty($city))
{
    mysql_query("UPDATE `users` SET `city` = '".$city."' WHERE `login` = '".$login."';");
}

use "INSERT INTO table (column1, column2,column3) VALUES (val1,val2,val3)";

ps: mysql_* is deprecated update to PDO or MySQLi

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