更新多行记录

时间:2013-09-19 07:03:28

标签: php mysql sql variables dreamweaver

首先,我使用PHP Dreamweaver使用phpMyAdmin数据库制作程序。当我提交按钮时,此程序将第一行的记录留空,并且行中的剩余记录仍然是第二行及以下,但第一行中的记录仍在数据库中,为什么会发生?任何想法让我解决这个程序,它也不会更新我的记录。这让我生病了,我需要帮助! TNX ...

<?php require_once('Connections/tlsc_conn.php'); 
  mysql_select_db($database_tlsc_conn, $tlsc_conn); 
  $query_Recordset1 = "SELECT * FROM tbl_name"; 
  $Recordset1 = mysql_query($query_Recordset1, $tlsc_conn) or die(mysql_error()); 
  $row_Recordset1 = mysql_fetch_assoc($Recordset1); 
  $totalRows_Recordset1 = mysql_num_rows($Recordset1); 

  if(isset($_POST['submit'])) { 
      $count = count($_POST['id']); 
      $submit = $_GET['id']; 

      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:lulu.php"); 
            exit; 
    }    
 } 
?>

<!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="">
  <table width="634" border="1">
    <tr>
       <td>id</td>
       <td>name</td>
       <td>lastname</td>
       <td>email</td>
    </tr>
   <?php do { ?> 
    <tr>
      <td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?> 
      <input name="id[]" type="hidden" value="<?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 } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>  
   </table>
    <p>
    <input type="submit" name="submit" value="Submit" />
    </p>
  </form>
   <p>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

将您的looping更改为while而不是do-while。然后在表单提交后检查生成的html和print_r($_POST),以查看post array的结构。现在不推荐使用mysqliPDO作为mysql

编辑:

<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { ?> 
<tr>
    <td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?> 
        <input name="id[]" type="hidden" value="<?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 }  ?>

答案 1 :(得分:0)

首先从第5行删除它:

  $row_Recordset1 = mysql_fetch_assoc($Recordset1); // I think this not valid it should be only in loop

并尝试使用while循环:

 <?php while($row_Recordset1 = mysql_fetch_assoc($Recordset1))
{
 ?> 
    <tr>
      <td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?> 
      <input name="id[]" type="hidden" value="<?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>
    <input type="submit" name="submit" value="Submit" />
    </p>
  </form>
   <p>
</body>