简体   繁体   English

PHP的MySQL批量记录更新?

[英]php mysql bulk records update?

I'm trying to update multiple mysql records, with this code: 我正在尝试使用以下代码更新多个mysql记录:

<strong>Update multiple rows in mysql</strong><br> 

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="test_mysql"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows 
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr> 
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this 
if($Submit){
   for($i=0;$i<$count;$i++){
     $sql1="UPDATE $tbl_name 
            SET name='$name[$i]', 
                lastname='$lastname[$i]', 
                email='$email[$i]' 
                WHERE id='$id[$i]'";
     $result1=mysql_query($sql1);
   }
}

if($result1){
header("location:update_multiple.php");
?>

It shows records from the DB in input text boxes, but when I change old records with new ones and then submit, nothing happens, page refreshes with old values. 它在输入文本框中显示了来自数据库的记录,但是当我用新记录更改旧记录然后提交时,什么也没发生,页面刷新时使用了旧值。

You will need to check if the form is submitted using $_POST['Submit'] . 您将需要检查是否使用$_POST['Submit']提交了表单。 Also you could use a hidden input field to keep track of each row to update: 您也可以使用隐藏的输入字段来跟踪要更新的每一行:

<input type="hidden" name="id[]" value="<?php echo $row['id']; ?>" />

Then when handling your submission you should do something like this: 然后,在处理提交内容时,您应该执行以下操作:

if(isset($_POST['Submit'])) {
   $ids = $_POST['id'];
   $names = $_POST['name'];
   $lastnames = $_POST['lastname'];
   $emails = $_POST['email'];
   //
   foreach($ids as $id) {
      // update the record based on the id and supplied data
   }
}

And of course the update process should be executed before you retrieve you rows from the database. 当然,应该在从数据库检索行之前执行更新过程。 In other words the above code should be placed near the top of your script. 换句话说,以上代码应放在脚本顶部附近。 At least before the: 至少在:

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

Submit should define that form if sent 如果发送,提交应定义该表格

move update code before select because you first view, and just after that update info, so you'll view previous results all the time 在选择之前先移动更新代码,因为您是第一次查看更新信息,并且在更新信息之后移动,因此您将始终查看以前的结果

...
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1

);
}

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows 
...

Using if($Submit){ is not the correct way to determinate if a form has been submitted ... 使用if($Submit){不是确定表单是否已提交的正确方法...

you should use : 您应该使用:

if($_SERVER['REQUEST_METHOD'] == "POST")

then process your submitted values. 然后处理您提交的值。

Note : you should update the DB before showing the values .... 注意:您应该在显示值之前更新数据库。

And please make sure you read about SQL Injection 并且请确保您了解有关SQL注入的信息

First of all mysql_query() doesn't return anything when you execute an update command. 首先,当执行update命令时, mysql_query()不返回任何内容。 You could use instead mysql_affected_rows to retrieve the number of rows changed by the previous query: 您可以改用mysql_affected_rows来检索上一个查询更改的行数:

http://php.net/manual/en/function.mysql-affected-rows.php http://php.net/manual/zh/function.mysql-affected-rows.php

Then, you should also do some refactoring like moving the update code before displaying the results, otherwise the header location directive won't work if you already sent output to the browser. 然后,还应该进行一些重构,例如在显示结果之前移动更新代码,否则,如果已经将输出发送到浏览器,则header location指令将不起作用。

did you apply the varaible names in single code? 您是否在单个代码中应用了易变的名称?

$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";

change above query to 将以上查询更改为

$sql1="UPDATE $tbl_name SET name='".$name[$i]."', lastname='".$lastname[$i]."', email='".$email[$i]."' WHERE id=$id[$i]";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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