简体   繁体   中英

how to update a table field with a variable value calculated

$average is the variable that i want should replace/update price field in a table. Table structure is:

and $key is the variablethrough which i get the nid of this table.


nid | sku     | price
7   |  prod-1 | 10
9   | prod-2  | 12

update query i am using is:


$query =db_query("UPDATE products a SET a.price = $average WHERE a.sku = $key");

but it gives an error:


PDOException: SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: 'tshirtm': UPDATE products a SET a.price = 1.75 WHERE a.sku = 7; Array ( ) in formModule_form_submit() (line 233 of D:\xampp\htdocs\olinestore\store\sites\all\modules\formModule\formModule.module).

I notice that the WHERE criteria is sku=7 -- however in your sample data, sku is a varchar? Was this intended on being nid?

Possibly the problem is your data type of your price column is an integer but you're trying to update it to a decimal. Depending on your RDBMS, this can easily be resolved.

Good luck.

When $key corresponds to nid, you shouldn't use sku. Try this:

$query =db_query("UPDATE products a SET a.price = $average WHERE a.nid = $key");

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