PHP多个更新记录无效 这个程序可以更新多个记录但只有一行,是否有必要修复或添加以使其工作? 我需要帮助... TNX
这是代码:
<?php require_once('Connections/tlsc_conn.php'); ?>
<?php
$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
mysql_select_db($database_tlsc_conn, $tlsc_conn);
$query_Recordset1 = "SELECT * FROM tb_exam";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $tlsc_conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
?>
答案 0 :(得分:1)
删除它并查看它是否更好
if($row_Recordset1){
header("location:mulupdate.php");
exit;
}
并在表单操作中将其设置为
action="mulupdate.php"
答案 1 :(得分:1)
$submit
在哪里设置?如果未设置,则您的更新将永远不会运行,$row_Recordset1
的值将始终是您的SELECT
查询的结果。这意味着header()
语句将立即重定向。
如果重定向到同一页面,您将获得循环。
您可能需要在代码顶部使用$submit = $_GET['submit']
,然后再使用
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]'";
$row_Recordset1=mysql_query($sql1);
}
// note: this moved inside if($submit) block
if($row_Recordset1){
header("location:mulupdate.php");
exit;
}
}
(我没有对此进行测试,因此逻辑可能需要进行调整)
答案 2 :(得分:1)
也未设置变量:
$name = $_POST['name'];
$lastname= $_POST['lastname'];
...
答案 3 :(得分:1)
试试这个,它应该适合你
<?php
require_once('Connections/tlsc_conn.php');
mysql_select_db($database_tlsc_conn, $tlsc_conn);
if(isset($_POST['submit'])) {
$count = count($_POST['name']);
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='$name[$i]',lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$row_Recordset1=mysql_query($sql1);
}
if($row_Recordset1){
header("location:mulupdate.php");
exit;
}
}
$query_Recordset1 = "SELECT * FROM tbl_name";
$Recordset1 = mysql_query($query_Recordset1, $tlsc_conn) or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form name="form2" method="post" action="">
<p> </p>
<table width="634" border="1">
<tr>
<td>id</td>
<td>name</td>
<td>lastname</td>
<td>email</td>
</tr>
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1) { ?>
<tr>
<td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?></td>
<td><input name="name[]" type="text" value="<?php echo $row_Recordset1['name']; ?>"></td>
<td><input name="lastname[]" type="text" value="<?php echo $row_Recordset1['lastname']; ?>"></td>
<td><input name="email[]" type="text" value="<?php echo $row_Recordset1['email']; ?>">
</td>
</tr>
<?php } ?>
</table>
<p>
<label>
<input type="submit" name="submit" value="Submit" />
</label>
</p>
</form>
</body>
</html>