如何将表中的多行更新为MySQL

时间:2017-06-20 14:01:21

标签: php mysql

首先,我尝试从MySQL检索多行数据并将其显示在表格中。

然后我尝试使用foreach循环将多行更新到MySQL但是徒劳无功。

如果有人有任何解决方案,将不胜感激。

include('connect-db.php');

$result = mysql_query("SELECT vaccinedetail.id,vaccinedetail.vaccineid,vaccinedetail.vaccinename1,vaccinedetail.vaccinename2,vaccinedetail.vaccinename3, vaccinedetail.totalnoofinjection,vaccinedetail.nthinjection,vaccinedetail.skip 
FROM vaccinedetail 
WHERE vaccinedetail.vaccineid = '" . $_POST['vaccineid'] . "'")
or die(mysql_error());?>

<?php while($row = mysql_fetch_array( $result )){?>

<tr>
<td><input type="text" name = "id[]" value="<?php echo $row['id'] ?>"</td>

<td><input type="text" name = "vaccineid[]" value="<?php echo $row['vaccineid'] ?>"</td>
<td><input type="text" name = "vaccinename1[]" value="<?php echo $row['vaccinename1'] ?>"</td>
<td><input type="text" name = "vaccinename2[]" value="<?php echo $row['vaccinename2'] ?>"</td>
<td><input type="text" name = "vaccinename3[]" value="<?php echo $row['vaccinename3'] ?>"</td>
<td><input type="text" name = "totalnoofinjection[]" value="<?php echo $row['totalnoofinjection'] ?>"</td>
<td><input type="text" name = "nthinjection[]" value="<?php echo $row['nthinjection'] ?>"</td>
<td><input type="text" name = "skip[]" value="<?php echo $row['skip'] ?>"</td>

</tr>

<?php }?>

</table>
<input type="submit" name="submit" value="Save change">
</form>


</div>

</body>
</html>

<?php

include('connect-db.php');

if (isset($_POST["submit"])){
    foreach ($_POST['vaccineid'] as $index => $vaccineid) {
        $data1 = mysql_real_escape_string($vaccineid);
        $data2 = mysql_real_escape_string($_POST['vaccinename1'][$index]);
        $data3 = mysql_real_escape_string($_POST['vaccinename2'][$index]);
        $data4 = mysql_real_escape_string($_POST['vaccinename3'][$index]);
        $data5 = mysql_real_escape_string($_POST['totalnoofinjection'][$index]);
        $data6 = mysql_real_escape_string($_POST['nthinjection'][$index]);
        $data7 = mysql_real_escape_string($_POST['skip'][$index]);

        mysql_query("UPDATE vaccinedetail SET vaccineid ='$data1', vaccinename1 = '$data2',vaccinename2 = '$data3',vaccinename3 = '$data4',
        totalnoofinjection = '$data5', nthinjection ='$data6', skip ='$data7'") or die(mysql_error()); 
    header("Location: start.php");
    }
}
?>
<html>

<head>
  <title>Display Vaccine type</title>
</head>

<body>
  <?php
session_start();?>
    <form action="" method="POST">
      <!-- Create the table and its heading-->

      <table border='1' cellpadding='10'>
        <tr>
          <th>ID</th>
          <th>Vaccine ID</th>
          <th>疫苗名稱 (繁體)</th>
          <th>疫苗名称 (简体)</th>
          <th>Vaccine Name (Eng)</th>
          <th>Total no of injection</th>
          <th>Nth Injection</th>
          <th>Next Injection Skip</th>
          <th></th>
          <th></th>
        </tr>

3 个答案:

答案 0 :(得分:0)

您的更新声明中没有WHERE子句:

mysql_query("UPDATE vaccinedetail SET vaccineid ='$data1', vaccinename1 = '$data2',vaccinename2 = '$data3',vaccinename3 = '$data4',
        totalnoofinjection = '$data5', nthinjection ='$data6', skip ='$data7'")
or die(mysql_error());

因此,您的update语句将使用上次执行的更新语句的值更新表中的所有记录。您应该为每条记录传递某种标识符。所以:

mysql_query("UPDATE vaccinedetail SET vaccineid ='$data1', vaccinename1 = '$data2',vaccinename2 = '$data3',vaccinename3 = '$data4',
        totalnoofinjection = '$data5', nthinjection ='$data6', skip ='$data7' WHERE ID = $id")

答案 1 :(得分:0)

嗯,这里有很多问题

  • 您没有在索引处更新。这意味着每个查询都会导致更新每一行。要解决此问题,请使用WHERE子句。这将限制行数。
foreach( $_POST['vaccineid'] as $index => $vaccineid ) {
    $id = mysql_real_escape_string($_POST['vaccineid'][$index]);
    /* Escape string queries */
    mysql_query(
    "UPDATE 
      vaccinedetail SET vaccineid ='$data1', 
      vaccinename1 = '$data2',
      vaccinename2 = '$data3',
      vaccinename3 = '$data4',
      totalnoofinjection = '$data5', 
      nthinjection ='$data6', 
      skip ='$data7' 
    WHERE id = $id") // <-- notice that I added a WHERE clause
        or die(mysql_error()); 
    header("Location: start.php");
}

另外:

  • 您在显示数据后更新数据。在if (isset($_POST["submit"])){之后将include('connect-db.php');移到右侧。
    • 在调用exit之后,您没有发出header声明(尽管您不希望这样做,但这会导致输出。)
    • 在您已将内容发送回浏览器后,您已header被调用(在header部分发送任何内容之前必须先调用?> <?php)。
    • 您正在使用mysql_库。这已被弃用了。使用mysqli_

答案 2 :(得分:0)

mysql_query("UPDATE vaccinedetail SET 
                            vaccineid ='".$data1."', 
                            vaccinename1 = '".$data2."',
                            vaccinename2 = '".$data3."',
                            vaccinename3 = '".$data4."',
                            totalnoofinjection = '".$data5."', 
                            nthinjection ='".$data6."', 
                            skip ='".$data7."' WHERE ID = $id;
                            ",$connectinObj) or die();

您尚未添加连接对象。