简体   繁体   English

使用我的表的PHP和MYSQL更新多行

[英]Update multiple row using PHP and MYSQL for my table

I want to create a php code which can update all the rows at once when i click the button. 我想创建一个php代码,当我单击按钮时可以一次更新所有行。 Already try few method that i look from the other post but still can't figure it out. 我已经尝试了几种从其他帖子中查找的方法,但仍然无法弄清楚。 Please help me................ 请帮我................

<table cellspacing="0" cellpadding="1" border=1px; action="" method="post">
<?php
        $result=mysqli_query($conn,"SELECT * from subject ");
        if($result->num_rows>0)
        {
            ?>
       <tr>
          <th>Subject Code</th>
          <th>Subject</th>
          <th>Fees</th>
          <th>Status</th>


        </tr>
        <?php
        while($row = mysqli_fetch_assoc($result))
        {
         if ($row["subject_status"]==="Available")
         {$status="Unavailable";}
          else $status="Available";
        ?>

         <tr class="alttr1">
          <td><?php echo $row["subject_code"]; ?></td>
          <td><?php echo $row["subject_name"]; ?></td>
          <td>RM<input type="number" name="price[]" value="<?php echo $row["subject_price"]; ?>" ></td>
          <td><select name="status[]" ><option value="<?php echo $row["subject_status"]; ?>" ><?php echo $row["subject_status"]; ?></option><option value="<?php echo $status; ?>" ><?php echo $status; ?></option></select></td>


        </tr>
        <?php 
        }
        ?>

      </table>
 <input type="submit" name="updatebtn" value="Save Changes">
<?php
if (isset($_POST["updatebtn"]))
{
    $price = $_POST["price"];
    $status = $_POST["status"];

    foreach($_POST["price"] as $price)
    {
    mysqli_query($conn,"UPDATE subject SET subject_price='$price' ");
    }

    foreach($_POST["status"] as $status)
    {
    mysqli_query($conn,"UPDATE subject SET subject_status='$status' ");
    }
    header("location: subjectmanage.php");

}

Add a hidden field containing the subject code to the form. 将包含主题代码的隐藏字段添加到窗体。

     <tr class="alttr1">
      <td><?php echo $row["subject_code"]; ?><input type="hidden" name="code[]" value="<?php echo $row["subject_code"]; ?>"></td>
      <td><?php echo $row["subject_name"]; ?></td>
      <td>RM<input type="number" name="price[]" value="<?php echo $row["subject_price"]; ?>" ></td>
      <td><select name="status[]" ><option value="<?php echo $row["subject_status"]; ?>" ><?php echo $row["subject_status"]; ?></option><option value="<?php echo $status; ?>" ><?php echo $status; ?></option></select></td>
    </tr>

Then the update code can look like this: 然后,更新代码如下所示:

<?php
if (isset($_POST["updatebtn"]))
{
    $codes = $_POST["code"];
    $prices = $_POST["price"];
    $statuses = $_POST["status"];

    foreach($codes as $i => $code)
    {
        $code = mysql_real_escape_string($code);
        $price = mysql_real_escape_string($prices[$i]);
        $status = mysql_real_escape_string($statuses[$i]);
        mysqli_query($conn,"UPDATE subject SET subject_price='$price', status = '$status' WHERE subject_code = '$code'");
    }
}

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

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