dongyuan9292 2017-11-02 05:53
浏览 53
已采纳

使用PHP和HTML表单更新多个SQL行

I have a table with rows in an SQL database that I want to be able to change the values for. I have built a form that has a select option built in so a row of data can be changed in the database. I want the ability to be able to change multiple rows at the same time in a database by just submitting one form. This is my code to create the form and the table with php. I can figure out how to get all the values over to the updatedb.php page, but from there I don't know how to update multiple rows in the database.

<form action="updatedb.php" method="POST">
<tbody>
 <?php
 $servername = "localhost";
 $username = "******";
 $password = "******";
 $dbname = "database";

 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
 }


 $sql = "SELECT * FROM table WHERE value = '$value'";
 $result = $conn->query($sql);

 if ($result->num_rows > 0) {
 // output data of each row
 while($row = $result->fetch_assoc()) {

 $test = $row["value3"];
 $test2 =$row["value5"];

 if ($row["status"] == "Value1") { $status1 = 'selected'; } else { $status1 = ''; }

 if ($row["status"] == "Value2") { $status2 = 'selected'; } else { $status2 = ''; }

 if ($row["status"] == "Value3") { $status3 = 'selected'; } else { $status3 = ''; }

 if ($row["status"] == "Value4") { $status4 = 'selected'; } else { $status4 = ''; }

 if ($row["status"] == "Value5") { $status5 = 'selected'; } else { $status5 = ''; }

 //This is the table that is created
 echo " 
 <tr><td>" . $row["value"] . $row["value2"] . "</td>
 <td>" . $row["value3"] . "</td>
 <td>" . $row["value4"] . "</td>
 <td>" . $row["value5"] . "</td>
 <td>" . $row["value6"] . "</td>
 <td>" . $row["value7"] . "</td>

<td>
 <select name='status[]' required>
  <option $status1 value='Status1'>Status1</option>
  <option $status2 value='Status2'>Status2</option>
  <option $status3 value='Status3'>Status3</option>
  <option $status4 value='Status4'>Status4</option>
  <option $status5 value='Status5'>Status5</option>
 </select>
 </td>

 <td>" . $row["value8"] . "</td></tr>";
 echo "
  <input name='send[]' value='$test' style='display: none' />". 
 "<input name='send2[]' value='$test2' style='display: none' />";
  }

  } else {
  //No data to show
  }

  $conn->close();

  ?>
 </tbody>
 <button type="submit">Submit</button>
 </form>
  • 写回答

1条回答 默认 最新

  • dongyuqi3808 2017-11-02 06:01
    关注

    In your updatedb.php file you get array of status,send and send2

    First of all you need id of records so you can add one hidden fields with id in your form like

    <input type="hidden" name="id[]" value="$row['id']">
    

    Than in your updatedb.php

    for($i=0;$i<count($_POST['id']);$i++){    
       $sql = "update tableName set status = '".$_POST['status'][$i]."',send = '".$_POST['send'][$i]."',send2 = '".$_POST['send2'][$i]."' where id = '".$_POST['id'][$i]."'";
       if(!mysql_query($sql)){
          echo "not updated".$_POST['id'][$i]; exit();  
       }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件