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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算