简体   繁体   English

如何在一次查询中使用计算更新数据库中的多行

[英]How to update multiple rows in database on one query with calculations

How should I change the code in order to update multiple rows at once, when I click on submit? 单击提交时,应如何更改代码以一次更新多行?

This is the code: 这是代码:

<html>
<title></title>
<head>
</head>
<body>
    <form action="" method="post">
     <table>
      <tr>
       <td><h6>Seatwork 1</h6></td>
       <td><h6>Seatwork 2</h6></td>
       <td><h6>Seatwork 3</h6></td>
       <td><h6>Quiz 1</h6></td>
       <td><h6>Quiz 2</h6></td>
     </tr>

     <?php




        $query1="SELECT * FROM tbl...";
        $query = mysql_query($query1) or die(mysql_error());
          while($user = mysql_fetch_assoc($query)){

              $userid = $user['userid'];
              $first = $user['first_name'];
              $last = $user['last_name'];
              $subject = $user['subject'];                                
              $sw1 = $user['sw1'];
              $sw2 = $user['sw2'];
              $sw3 = $user['sw3'];
              $qz1 = $user['qz1'];
              $qz2= $user['qz2'];



            ?>  
        <tr>
          <td><h6><?php echo $first; ?>&nbsp;<?php echo $last; ?></h6></td>
            <!-- Written Works -->
          <td><input type="text" name="sw1" value="<?php echo $ws1; ?>"></td>
          <td><input type="text" name="sw2" value="<?php echo $ws2; ?>"></td>
          <td><input type="text" name="sw3" value="<?php echo $ws3; ?>"></td>
          <td><input type="text" name="qz1" value="<?php echo $ws4; ?>"></td>
          <td><input type="text" name="qz2" value="<?php echo $ws5; ?>"></td>
       </tr>

       <?php } ?>
     </table>

       <input type="submit" name="submit" value="submit">

</form>

Query for saving the rows: 查询以保存行:

It saves but only if the value of the columns in a row are the same. 仅在行中的列值相同时保存。 Please help me solve this problem. 请帮我解决这个问题。 Thanks In advance. 提前致谢。

<?php  

    if ($_POST["submit"]) {

       $sw1 = $_POST['sw1'];
       $sw2 = $_POST['sw2'];
       $sw3 = $_POST['sw3'];
       $qz1 = $_POST['qz1'];
       $qz2 = $_POST['qz2'];

       $WST = $sw1 + $sw2 + $sw3 + $qz1 + $qz2; 
       $WSP1 = ($WST / 150 )*100;
       $WSP =  number_format((float)$WSP1, 2, '.', '');


       $sql="UPDATE tbl_quarter1  set sw1 = '$sw1' , sw2 = '$sw2', 
             sw3 = '$sw3', qz1 = '$qz1', qz2 = '$qz2', 
             WST='$WST', WSP='$WSP'";
       mysql_query($sql) or die(mysql_error());
   }
?>

</body>
</html>

In the form you need to pass the firstname and lastname 在表格中,您需要传递名字姓氏

<input type="hidden" name="first_name" value="<?php echo $first; ?>" />
<input type="hidden" name="last_name" value="<?php echo $last; ?>" />

And in the processing script you could update the rows with respect to the firstname and lastname 并且在处理脚本中,您可以更新有关名字姓氏的行

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];

$sql="UPDATE tbl_quarter1  set sw1 = '$sw1' , sw2 = '$sw2',
      sw3 = '$sw3', qz1 = '$qz1', qz2 = '$qz2', WST='$WST', 
      WSP='$WSP' WHERE first_name = '$first_name' AND 
      last_name = '$last_name' ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM