douhui3305 2017-07-09 18:22
浏览 30
已采纳

使用PHP将传入的数字添加到MySQL单元格

I am trying to add a number to a number already in a MySQL table. I am using POST to pass in the number.

    <?php
    require "connection.php";

    $id = $_POST["id"];
    $number = $_POST["number"];

Do I have to make 2 separate queries, one to retrieve the current value and set is as a variable and one to insert the new value by adding the number to the variable or can I accomplish this with one query with something like this:

    $update_query = "update Table set value = (value + $number) where id like '$id';" 
  • 写回答

2条回答 默认 最新

  • doutao5419 2017-07-09 18:32
    关注

    update field to add value to existing value:

    UPDATE table SET value = value + $number WHERE id = $id;
    

    update field to contact value to existing value:

    use the CONCAT function :

    UPDATE table SET value= CONCAT('$number',value) WHERE id=$id;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容