简体   繁体   中英

Updating multiple SQL rows with PHP and HTML form

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>

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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