[英]How to update multiple rows in a table to MySQL
First I tried to retrieve multiple rows of data from MySQL and display them in a table. 首先,我尝试从MySQL检索多行数据并将其显示在表中。
Then I tried to use foreach loop to update multiple rows into the MySQL but in vain. 然后,我尝试使用foreach循环将多个行更新到MySQL中,但没有成功。
If anyone has any solution would be greatly appreciated. 如果有人有任何解决方案,将不胜感激。
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>
You haven't got a WHERE clause in your update statement: 您的update语句中没有WHERE子句:
mysql_query("UPDATE vaccinedetail SET vaccineid ='$data1', vaccinename1 = '$data2',vaccinename2 = '$data3',vaccinename3 = '$data4',
totalnoofinjection = '$data5', nthinjection ='$data6', skip ='$data7'")
or die(mysql_error());
So your update statement will update all records in the table with the values of the last update statement executed. 因此,您的update语句将使用最后执行的update语句的值来更新表中的所有记录。 You should pass through some kind of identifier for each record. 您应该为每条记录传递某种标识符。 So: 所以:
mysql_query("UPDATE vaccinedetail SET vaccineid ='$data1', vaccinename1 = '$data2',vaccinename2 = '$data3',vaccinename3 = '$data4',
totalnoofinjection = '$data5', nthinjection ='$data6', skip ='$data7' WHERE ID = $id")
Well, there are a number of issues here 好吧,这里有很多问题
WHERE
clause. 要解决此问题,请使用WHERE
子句。 That will limit the number of rows. 那将限制行数。 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");
}
In addition: 此外:
if (isset($_POST["submit"])){
to right after include('connect-db.php');
将if (isset($_POST["submit"])){
移到include('connect-db.php');
. 。
exit
statement after the call to header
(this will lead to output despite the fact that you don't want it to do that.) 您不会在对header
的调用之后放置exit
语句(尽管您不希望它执行此操作,但这仍会导致输出。) header
called after you've already sent content back to the browser ( header
must be called before any content is sent in a ?> <?php
section). 你已经header
叫你发出后的内容返回给浏览器( header
在发送任何内容之前必须调用?> <?php
部分)。 mysql_
library. 您正在使用mysql_
库。 That's deprecated. 不推荐使用。 Use mysqli_
. 使用mysqli_
。 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();
you have not add connection object. 您尚未添加连接对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.