我正在尝试使用复选框删除多行。但价值观没有删除。 我不明白我哪里错了。 请帮助我。
Connect.php
<?php
$host="localhost"; // Host name
$username="DB_USERNAME"; // Mysql username
$password="DB_PASSWORD"; // Mysql password
$db_name="DB_NAME"; // Database name
$tbl_name="TABLE_NAME"; // 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");
?>
exe.php
<?php
include "connect.php";
// Check if delete button active, start this
if($_POST['delete'])
{
$id = $_POST['data'];
$count = count($id);
for($i=0;$i<$count;$i++){
//echo "<br> value = ".$id[$i]."Jumlah = ".$count ;
$sql = "DELETE FROM $tbl_name WHERE id='$id[$i]'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";}
}
mysql_close();
?>
的index.php
<?php
include "connect.php";
$result=mysql_query("SELECT * FROM $tbl_name ORDER BY ts DESC");
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="exe.php">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </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>Order Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Type</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="data[]" type="checkbox" id="data" value="<?php echo $rows['oid']; ?>">
</td>
<td bgcolor="#FFFFFF"><?php echo $rows['oid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['user']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['data']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['type']; ?></td>
</tr>
<?php
}unset($rows);
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
有人请帮帮我,告诉我哪里错了。我还在学习PHP。
答案 0 :(得分:0)
您可以使用内置的SQL WHERE IN
子句,而不是使用循环运行多个SQL查询:
$ids = join(',',$id); //same as using implode()
$sql = "DELETE FROM $tbl_name WHERE id IN '$ids'";
效率更高!
希望这有帮助!
答案 1 :(得分:0)