简体   繁体   English

如何使用计算出的变量值更新表字段

[英]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. $ average是我想要替换/更新表中价格字段的变量。 Table structure is: 表结构为:

and $key is the variablethrough which i get the nid of this table. 和$ key是变量,通过它我可以获得此表的编号。


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? 我注意到WHERE条件是sku = 7-但是在您的示例数据中,sku是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. 根据您的RDBMS,可以轻松解决此问题。

Good luck. 祝好运。

When $key corresponds to nid, you shouldn't use sku. 当$ key对应于nid时,您不应使用sku。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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