douyi1966 2011-12-10 19:39
浏览 23

如何使用PHP增加MySQL数据库中的某些行?

I have this PHP:

$adj_index = $currentSignup + 1 - $r;//$r=3 and $currentSignup=24
for($i=1; $i<$referrals; $i++){
    $current_index = $currentSignup + 1 - $i;
    $q = "SELECT signup_id FROM app_sign_ups WHERE (adjusted_index='$current_index' AND app_id='$app_id')";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));

    $next_index = $current_index + 1;
    if (mysqli_num_rows($r) == 1){
        $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
        $signuper = $row['signup_id'];
        $q = "UPDATE app_sign_ups SET adjusted_index='$next_index' WHERE (app_id='$app_id' AND signup_id='$signuper')";
        $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));
    }

}

$q = "UPDATE app_sign_ups SET adjusted_index='$adj_index' WHERE app_s_id='$app_s_id'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q
<br />MySQL Error: " . mysqli_error($dbc));

It is supposed to update rows in my database. Each row has a value (adjusted_index) from 1 to 25 in order. With it I am taking number 25, making it 22 (by removing $r from the original number [25]) and moving the previous 22-24 up one each (so to 23, 24, 25). For some reason when I run it is resulting in 22, 24, 25, 22 instead of 23, 24, 25, 22 like I want it. I have done it on a couple other combinations and it seems that the row that has the adjusted_index that is being replaced by the last row (22 being replaced by 25).

  • 写回答

1条回答 默认 最新

  • douyan9398 2011-12-10 21:48
    关注

    Run only one UPDATE statement, once:

    $q = " UPDATE app_sign_ups 
           SET adjusted_index = adjusted_index 
                              + CASE WHEN adjusted_index < 25
                                     THEN 1
                                     ELSE - '$r' 
                                END  
           WHERE adjusted_index >= 25 - '$r' 
             AND app_id = '$app_id'
           ORDER BY adjusted_index 
         ";
    

    Or, even better, with 2 UPDATE statements:

    $q1 = " UPDATE app_sign_ups 
            SET adjusted_index = adjusted_index + 1
            WHERE adjusted_index >= 25 - '$r' 
              AND app_id = '$app_id'
            ORDER BY adjusted_index DESC              --- this line is needed if
         ";                                           --- you have a UNIQUE index on
                                                      --- (app_id, adjusted_index)
    
    $q2 = " UPDATE app_sign_ups 
            SET adjusted_index = 25 - '$r'
            WHERE adjusted_index = 26 
              AND app_id = '$app_id'
         ";
    
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上