2011-12-17 143 views
-1

下面是我的代码如下。问题是当我尝试更新信息时,它会清除所有记录并且不会更新。我怎样才能让这个脚本更新并且不清楚。此外,我以前使用过这个,它运行良好,但突然它不..我可能已经删除了一些重要的东西。用php更新mysql中的多行

<strong>Update multiple rows in mysql</strong><br> 

<?php 
$mysql_host = "mysql.com"; 
$mysql_user = "username"; 
$mysql_pass = "password"; 
$mysql_database = "dbname"; 
$tbl_name="test_mysql"; // Table name 

// Connect to server and select databse. 
mysql_connect("$mysql_host", "$mysql_user", "$mysql_pass")or die("cannot connect"); 
mysql_select_db("$mysql_database")or die("cannot select DB"); 

$sql="SELECT * FROM $tbl_name"; 
$result=mysql_query($sql); 

// Count table rows 
$count=mysql_num_rows($result); 
$id = array(); 
?> 
<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>Lastname</strong></td> 
<td align="center"><strong>Email</strong></td> 
</tr> 
<?php 
while($rows=mysql_fetch_array($result)){ 
?> 
<tr> 
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> 
<td align="center"><input name="name[]" type="text" id="name" value="<? echo  $rows['name']; ?>"></td> 
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td> 
<td align="center"><input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>"></td> 
</tr> 
<?php 
} 
?> 
<tr> 
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> 
</tr> 
</table> 
</td> 
</tr> 
</form> 
</table> 
<?php 
// Check if button name "Submit" is active, do this 
if(isset($_POST['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]'"; 
$result1=mysql_query($sql1); 
} 
} 

if($result1){ 
echo "Good"; 
////header("location:update_multiple.php"); 
} 
mysql_close(); 
?> 

回答

2

您已经使用错误的一组变量,
尝试

$name[$i]   <-- access local variable, an array called $name 
$_POST["name"][$i] <-- access $_POST, the form name instead 

我会建议你使用$row["id"]作为索引键(name[$row["id"]]),
而不是使用连续索引(键(0 ,1,2 ...)

0

你的SQL中没有定义任何变量($ name,$ lastname,$ email,$ id)。

在for循环中,使用$ _POST ['name'] [$ i]等等。

此外,它似乎是你忘记把你的身份证在某种形式(隐藏)领域?

0

试试这个:

<?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']); 
// $count=mysql_num_rows($Recordset1); 
    $submit = $_GET['submit']; 
$i = ($_POST['count']); 
$name = ($_POST['name']); 
$lastname = ($_POST['lastname']); 
$email = ($_POST['email']); 
$id = ($_POST['id']); 

    for($i=0;$i<$count;$i++){ 

      $sql1="UPDATE $tbl_name SET name='{$_POST['name'][$i]}', 
           lastname='{$_POST['lastname'][$i]}', 
            email='{$_POST['email'][$i]}' 
           WHERE id='{$_POST['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> 


    </p> 

</body> 
</html>