MYSQL INT另外

时间:2014-02-12 09:43:16

标签: php mysql

我正在尝试从mysql中获取INT值并进行添加,最后更新数据库。但这似乎没有做更新?我该如何解决这个问题?

$resultSecond = mysql_query("SELECT * FROM Furniture");
while($lineSecond = mysql_fetch_array($resultSecond)) {
    $item     = $lineSecond["Item"]; 
    $janD     = $lineSecond["January"];
    $febD     = $lineSecond["February"];
    $marD     = $lineSecond["March"];
    $pastVals = $lineSecond["yearSale"];
    $totalCT  = $lineSecond["monthSale"];

    $totThisM = ($janD + $febD + $marD + $pastVals + $totalCT);
    mysql_query("UPDATE Furniture SET Furniture = '".$totThisM."' WHERE Item ='$item' LIMIT 1");
}

2 个答案:

答案 0 :(得分:2)

为什么不直接使用MySQL?

UPDATE `Furniture`
SET `Furniture` = (`January`+`February`+`March`+`yearSale`+`monthSale`) 

测试数据

+---------+---------+-------+
| number1 | number2 | total |
+---------+---------+-------+
|       1 |       5 |     0 |
|       2 |       3 |     0 |
+---------+---------+-------+

我运行我的查询;

UPDATE table SET total = (`number1`+`number2`);

表格已更新;

+---------+---------+-------+
| number1 | number2 | total |
+---------+---------+-------+
|       1 |       5 |     6 |
|       2 |       3 |     5 |
+---------+---------+-------+

备注

  • 在CodeBird发表评论后删除了WHERE条款。
  • 来自Ravinder的评论后调整后的查询

答案 1 :(得分:0)

试试这个:

mysql_query("UPDATE Furniture SET Furniture = '". $totThisM ."' WHERE Item ='". $item ."' LIMIT 1");

修改

不正确的答案,请参阅评论以了解详情。