Mysql查询的案例条件

时间:2017-05-12 12:31:23

标签: php mysql mysqli

我遇到了问题。我找不到错误。我有一个数据库名称测试。有一些字段就像Id,cl1,cl2,cl3 .......我的期望是当我选择(使用复选框)时,一些id数据库表字段(cl1)将更新1,其余更新为3。 我认为它可以让我对工作有所了解。这是我的数据库的屏幕截图。 https://www.dropbox.com/s/meijmf69dkkb5o2/db.JPG?dl=0 这是我的界面的截图。 https://www.dropbox.com/s/fc1b186ly0mda8u/interface.JPG?dl=0 当我选择id 1001,1003时。它会显示不同的结果 这是我的代码:我很长时间都在尝试。

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);

     }

}

在HTML部分

   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>

0 个答案:

没有答案