doumou1864 2016-01-27 18:02
浏览 5

如何继续在PHP中增加价值

<?php
    $con=mysqli_connect("localhost","xx","xx","xx");

$con2=mysqli_connect("localhost","xx","xx","xx");

    $name = $_POST["name"];
    $state = $_POST["state"];
    $feedback = $_POST["feedback"];
    //$ad_id= $_POST["ad_id"];
    $rate= $_POST["rate"];

    $ad_rating = $_POST["ad_rating"];
    $ad_id= $_POST["ad_id"];

    $statement = mysqli_prepare($con, "INSERT INTO user_feedback(name, state, feedback, ad_id, rate) VALUES (?,?,?,?,?)");

    $statement2 = mysqli_prepare($con, "UPDATE post_ads SET ad_rating= $ad_rating + $ad_rating WHERE ad_id= ?");

        mysqli_stmt_bind_param($statement, "sssss", $name, $state, $feedback, $ad_id, $rate);

    mysqli_stmt_bind_param($statement2, "ss", $ad_rating, $ad_id);

        mysqli_stmt_execute($statement);

        mysqli_stmt_close($statement);

        mysqli_stmt_execute($statement2);

        mysqli_stmt_close($statement2);

        mysqli_close($con);


?>

Database example

I currently doing a mobile apps and need to parse data into database and here is my php code.

i currently have ad_rating attribute inside my database table and i have attach the image link here.

My problem is that every time i update new value of ad_rating, how should i add it with previous one? Example ad_rating = 4, so when i try to do a new request by sending ad_rating = 3, i want to add it with my previous ad_rating value which is 4, so the value inside ad_rating become 7.

  • 写回答

2条回答 默认 最新

  • duanqian2368 2016-01-27 18:06
    关注

    You can use an insert statement:

    insert into post_ads
    (ad_id, ad_rating)
    values (?,?)
    on duplicate key update ad_rating=values(ad_rating)+ad_rating;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000