简体   繁体   中英

Update multiple rows in mysql with php

Here is my code below. The problem is when I try to update info it instead clears all records and does not update. How can I get this script to update and not clear. Also, I have used this before and it worked fine but all the sudden it doesn't.. I might have removed something important.

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

You have using wrong set of variables,
try

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

I would suggest you make use $row["id"] as index key ( name[$row["id"]] ),
instead of using sequential indexed (key (0, 1, 2...)

try this:

<?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']}'";

None of your variables are defined in your SQL ($name, $lastname, $email, $id).

In your for loop, use $_POST['name'][$i] and so forth.

Also, it seems like you have forgotten to put your id in some form (hidden) field?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM