php 如何更新php中的多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18929978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:30:20 来源:igfitidea点击:
how to update multiple rows in php
提问by user2774977
<?php
$host="localhost"; // Host name
$username="xxxx"; // Mysql username
$password="xxxx"; // Mysql password
$db_name="xxxx"; // Database name
$tbl_name="xxxx"; // 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 xxxx WHERE branch = 'xx' AND xxxx.semester=x ORDER BY xxxx.xxxx";
$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>may_tc_s1</strong></td>
<td align="center"><strong>may_ac_s1</strong></td>
</tr>
<?php
// Check if button name "submit" is active, do this
if (isset($_POST['Submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET xxx='$name[$i]', xxx='$lastname[$i]', xxx='$email[$i]' WHERE xxx='$id[$i]'";
$result1=mysql_query($sql1);
}
}
if(isset($result1)){
header("location:try.php");
}
?>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['idatten']; ?><?php echo $rows['idatten']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<?php echo $rows['username']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['may_tc_s1']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<?php echo $rows['may_ac_s1']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
I am able to see the records in the text fields but the new entered values are not getting updated????? Is there any thing wrong with the code????? Tried with various methods and could'nt find any correct methods???
我能够看到文本字段中的记录,但新输入的值没有更新????代码有问题吗????尝试了各种方法,找不到正确的方法???
回答by Simbu
Refer this code:
参考这个代码:
extract($_POST);
if (isset($Submit))
{
for($i=0;$i<$count;$i++)
{
$update=("UPDATE tbl_name SET name='$name[$i]', lname='$lastname[$i]',email='$email[$i]' WHERE id='$id[$i]'");
$res=mysql_query($update);
}
}
if(isset($res))
{
header("location:try.php");
}
?>
<?php
while($rows=mysql_fetch_array($result)){
?>
回答by Ashwini Agarwal
You are not quoting variables properly.
您没有正确引用变量。
$sql1 = "UPDATE $tbl_name SET xxx='".$name[$i]."', xxx='".$lastname[$i]."', xxx='".$email[$i]."' WHERE xxx='".$id[$i]."'";