簡體   English   中英

用php更新mysql中的多行

[英]Update multiple rows in mysql with php

下面是我的代碼。 問題是當我嘗試更新信息時,它會清除所有記錄並且不更新。 我怎樣才能讓這個腳本更新而不是清除。 另外,我以前用過這個,它工作得很好,但突然之間它沒有......我可能已經刪除了一些重要的東西。

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

您使用了錯誤的變量集,
嘗試

$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...)

嘗試這個:

<?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>

使用此命令更改數據庫中的多個條目:

$sql = "UPDATE users SET name  = ?, lastname = ?, email = ? WHERE id = '{$_SESSION['id']}'";

您的 SQL 中沒有定義任何變量($name、$lastname、$email、$id)。

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

另外,您似乎忘記將您的 id 放在某種形式(隱藏)字段中?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM