更新多页的代码如下:
<title>Update Multiple Data Form</title>
<?php
include ('config.php');
$tbl_name="user";
$id=$_POST['id'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="300" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="300" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>First Name</strong></td>
<td align="center"><strong>Last Name</strong></td>
<td align="center"><strong>Email-Id</strong></td>
<td align="center"><strong>Mobile No</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input name="firstname[]" type="text" id="firstname" value="<?php echo $rows['firstname']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>">
</td>
<td align="center">
<input name="mobile[]" type="text" id="mobile" value="<?php echo $rows['mobile']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
$id=$_POST['id'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$mobile=$_POST['mobile'];
if(isset($_POST['submit'])){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET firstname='$firstname', lastname='$lastname', email='$email', mobile='$mobile' WHERE id='$id'";
$result1=mysql_query($sql1);
}
}
if($result1){
}
mysql_close();
?>
<a href="list.php">Click here to List Data</a>
我给出的值不是更新,一旦我提交值,页面只是重新加载没有做任何事情。我不知道我在哪里犯了实际的错误,错误不在于我对这个页面采取行动。
我浏览了这个页面PHP MySQL Update Set query with Multiple columns和几页。它没有解决我的问题。
答案 0 :(得分:0)
在所有参数中传递值[$ i]时,你犯了一个小错误。
$sql1="UPDATE $tbl_name SET firstname='$firstname[$i]', lastname='$lastname[$i]', email='$email[$i]', mobile='$mobile[$i]' WHERE id='$id[$i]'";
答案 1 :(得分:0)
您的$ _POST [&#39; id&#39;],$ _ POST [&#39;名字&#39;]等等。数组不是单个值,请打印POST请求您将获得清晰的图片。 使用以下sql:
$sql_update ="UPDATE $tbl_name SET firstname='$firstname[$i]', lastname='$lastname[$i]',email='$email[$i]', mobile='$mobile[$i]' WHERE id='$id[$i]'";
还从哪里获得计数值?它必须使用任何隐藏字段通过表单发布。
答案 2 :(得分:0)
试试这个。你的sql字符串错误地使用单引号。
for($i=0;$i<$count;$i++){
$sql1 =<<<QRY
UPDATE {$tbl_name} SET firstname="{$firstname[$i]}", lastname="{$lastname[$i]}", email="{$email[$i]}", mobile="{$mobile[$i]}" WHERE id="{$id[$i]}"
QRY;
$result1=mysql_query($sql1);
}
答案 3 :(得分:0)
首先,对于不同的输入字段,您不应具有相同的ID。 (例如)
<input name="id[]" type="text" id="id" value="<?php echo $rows['id']; ?>">
应该更改此ID。
试试这样:
if(isset($_POST['submit'])){
for($i=0;$i<$count;$i++){
$id=$_POST['id'][$i];
$firstname=$_POST['firstname'][$i];
$lastname=$_POST['lastname'][$i];
$email=$_POST['email'][$i];
$mobile=$_POST['mobile'][$i];
$sql1="UPDATE $tbl_name SET firstname='$firstname', lastname='$lastname', email='$email', mobile='$mobile' WHERE id='$id'";
$result1=mysql_query($sql1);
if(!$result1){
die("can not updated: ".mysql_error());
}
}
}
mysql_close();
?>
您也不应使用mysql_*
,因为不推荐使用所有mysql_*
函数。