duanqianruan8448 2017-05-12 12:31
浏览 63
已采纳

Mysql查询的案例条件

I am facing a problem. I could not find the error. I have a database name test. There are some field on that like Id, cl1, cl2,cl3....... My expectation is when I select(using checkbox) some id database table field(cl1) will updated by 1 and rest of updated by 3. I think it can make some idea of my job. Here is the screenshot of my database. https://www.dropbox.com/s/meijmf69dkkb5o2/db.JPG?dl=0 Here is the screenshot of my Interface. https://www.dropbox.com/s/fc1b186ly0mda8u/interface.JPG?dl=0 When I select id 1001,1003. It will show different result Here is my code: I am trying it for long time.

if (isset($_POST["sub"])) {
 $values = $_POST['check'];
 $max=sizeof($values);
 $valuesArr = array();
 foreach ($values as $a){
 $valuesArr[] = mysql_real_escape_string($a);
                      }
 for ($i=0; $i <$max ; $i++) { 
  $query2="UPDATE test SET cl1 = CASE WHEN Id = '$valuesArr[$i]' THEN 1 ELSE 3 END";
 $result_upp=mysqli_query($con,$query2);

     }

}

In HTML part

   while($row = mysqli_fetch_array($result2))
       {
        ?>
       <tr class="success">
          <td> <?php echo $row['Id'] ?> </td>
         <td >&nbsp&nbsp<input type="checkbox" name="check[]" class="chk_val" value="<?php echo $row['Id']?>"/></td>

        <td style="background-color: <?php echo ($row['cl1'] == "1") ? "green": "#FF4500" ?>; color:<?php echo ($row['cl1'] == "1") ? "green": "#FF4500" ?>; "> 
        <?php echo $row['cl1'] ?> 
         </td>
        <td style="background-color: <?php if($row['cl2'] == "1")echo "green"; if($row['cl2'] == "2")echo "gainsboro"; if($row['cl2'] == "3")echo "yellow" ?>; color:<?php if($row['cl2'] == "1")echo "green"; if($row['cl2'] == "2")echo "gainsboro"; if($row['cl2'] == "3")echo "yellow" ?>; ">  
         <?php echo $row['cl2'] ?> 
          </td>
        <td style="background-color: <?php echo ($row['cl3'] == "1") ? "green": "#FF4500" ?>; color:<?php echo ($row['cl3'] == "1") ? "green": "#FF4500" ?>; ">  
        <?php echo $row['cl3'] ?> 
        </td>
       <td style="background-color: <?php echo ($row['cl4'] == "1") ? "green": "#FF4500" ?>; color:<?php echo ($row['cl4'] == "1") ? "green": "#FF4500" ?>; ">  
        <?php echo $row['cl4'] ?> 
       </td>
      <td style="background-color: <?php echo ($row['cl5'] == "1") ? "green": "#FF4500" ?>; color:<?php echo ($row['cl5'] == "1") ? "green": "#FF4500" ?>; ">  
      <?php echo $row['cl5'] ?> 
         </td>

       <?php         
           }
        ?>
    </tr>
   </tbody>            
   </table>
   <div class="pull-right">
   <input type="submit" name="sub" class="btn btn-info" value="Submit Attendance">
     </div>
     </form>
  • 写回答

2条回答 默认 最新

  • doulan4939 2017-05-12 13:02
    关注

    In you loop for ($i=0; $i <$max ; $i++), it looks like you are trying to update the rows one at a time to either 1 or 3.

    But what your query is ACTUALLY doing, is updating all the rows in your database on every iteration of your loop.

    In the last iteration of your loop, the corresponding row will be updated to have cl1 = 1, and EVERY other cl1 = 3, overiding any/all previous updates.

    Try this instead:

    if (isset($_POST["sub"])) {
        $values = $_POST['check'];
        $max=sizeof($values);
        $valuesArr = array();
    
        foreach ($values as $a){
            $valuesArr[] = "'".mysql_real_escape_string($a)."'";}  //I added single quotes, because mysql_real_escape_string() only works on string
    
        $query2 = "UPDATE test SET cl1 = CASE WHEN Id in (".implode(',', $valuesArr).") THEN 1 ELSE 3 END";
        $result_upp=mysqli_query($con,$query2);  //run this query once to update all rows to the correct value
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False