-1

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);

?>
4

4 回答 4

1

Where is $submit set? If it's not set then your update will never run and the value of $row_Recordset1 will always be the result of your SELECT query. This means in turn that the header() statement will redirect immediately.

If the redirection is to the same page you will have the loop.

You probably need $submit = $_GET['submit'] at the top of your code and later use

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;
  }
}

(I haven't tested this so the logic might need a tweak)

于 2013-09-18T07:14:10.930 回答
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>&nbsp;</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>
于 2013-09-18T08:00:49.127 回答
1

变量也没有设置:

$name = $_POST['name'];
$lastname= $_POST['lastname'];
...
于 2013-09-18T07:19:56.283 回答
1

删除它,看看它是否更好

if($row_Recordset1){
header("location:mulupdate.php");
exit;
}

并在表单操作中,将其设置为

action="mulupdate.php"
于 2013-09-18T07:08:58.330 回答