在php我的sql中删除多条记录

时间:2009-12-30 15:02:31

标签: php mysql

 <?php


$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="toybox"; // Database name 
$tbl_name="Emp"; // Table name 


// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

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

$count=mysql_num_rows($result);

// echo $count;

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['EmpId']; ?>"></td>
 <td bgcolor="#FFFFFF"><?php echo $rows['EmpId']; ?></td>
 <td bgcolor="#FFFFFF"><?php echo $rows['FirstName']; ?></td>
 <td bgcolor="#FFFFFF"><?php echo $rows['LastName']; ?></td>
 <td bgcolor="#FFFFFF"><?php echo $rows['Email']; ?></td>
</tr>
<?php
}
?>
<tr>
 <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php
// Check if delete button active, start this 
if($delete){
 for($i=0;$i<$count;$i++){
  $del_id = $checkbox[$i];
  $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  $result = mysql_query($sql);
 }

// if successful redirect to delete_multiple.php 
 if($result){
  echo " Record have been deleted";
 }
}
mysql_close();
?>

</table>
</form>
</td>
</tr>
</table>

2 个答案:

答案 0 :(得分:5)

我不是对你的删除循环的每次迭代进行查询,而是将所有索引构建成一个字符串,并使用如下内容:

DELETE FROM tableName 
WHERE id IN (1,2,12,53)

此外,您的提交按钮不会以$delete形式显示,而是$_POST["delete"]。和你的联系:

mysql_connect("$host", "$username", "$password")

你真的不应该像字符串那样使用变量(通常) - 这应该写成:

mysql_connect($host, $username, $password)

此外,你的删除逻辑中还有一些问题。例如,我已经指出,一旦在服务器上注册,您的<input type='submit' name='delete' />按钮就会被称为$_POST["delete"]。同样,ID值为checkbox[]的复选框在服务器上只会被称为$_POST["checkbox"]

此外,您的删除逻辑中使用的$count变量基于先前的查询,该查询选择了显示它们的所有记录。它不反映要删除的复选框的数量,它反映了显示的记录数。因此,你的for循环不应该基于它:

for ($i = 0; $i < count($_POST["checkbox"]); $i++)
{
  // delete $_POST["checkbox"][$i];
}

同样,我建议您构建一个值字符串并运行单个查询而不是多个查询。

答案 1 :(得分:1)

Working Code .. Consider Point 1 2 and 3

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("funconnect") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM members") 
or die(mysql_error());  
$count=mysql_num_rows($result);

echo "<form name='sendmail' method='post' action='memberList.php'><table border='1'>";
echo "<tr> <th>Select</th> <th>Name</th> </tr>";
// keeps getting the next row until there are no more to get
$countSn = 0;
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    $chkname = "checkbox".$countSn; #Point 1- Create defferent name for checkboxex like checkbox0, checkbox1 
    echo "<tr><td><input type='checkbox' name=".$chkname." value=".$row['m_id']." /></td>";

    echo "</td><td>"; 
    echo $row['m_name'];
    echo "</td></tr>"; 
    $countSn++;
} 
echo '<tr><td colspan=2><input name="delete" type="submit" id="delete" value="Delete"></td></tr></table></form>';
$delete=$_POST['delete'];
$checkbox=$_POST['checkbox'];
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
    $cname = "checkbox".$i;# Point 2- Create check box name like checkbox0, checkbox1
    $checkbox=$_POST[$cname]; #Point 3 - Retrieve data against name
    echo $i."===".$checkbox."<br />";
    //echo $del_id;
//$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
//$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
//echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>