-6ren">
gpt4 book ai didi

php - 使用复选框从 mysql 中删除多行

转载 作者:行者123 更新时间:2023-11-29 08:07:33 25 4
gpt4 key购买 nike

我需要使用复选框从 mysql 中删除多行。有人可以帮我找出代码中的问题吗

 <?php

$host="localhost"; // Host name
$username="xxxx"; // Mysql username
$password="xxxx"; // Mysql password
$db_name="xxxx"; // Database name
$tbl_name="xxx"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Retrieve data from database
$sql="SELECT * FROM $tbl_name ORDER BY id ASC";
$result=mysql_query($sql);
// num rows
$count=mysql_num_rows($result);
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>


<table id="rows" width="100%" border="1" bordercolor="#000" cellspacing="1" cellpadding="3" cellwidth="100" float="left" margin-left="20px" >
<tr>
<td width="2%" align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td width="1%" BGCOLOR="#fff"><? echo $rows['id']; ?></td>
<td width="22%" BGCOLOR="#fff"><? echo $rows['location']; ?></td>
<td width="22%" BGCOLOR="#fff"><? echo $rows['type']; ?></td>
<td width="22%" BGCOLOR="#fff"><? echo $rows['qnty']; ?></td>
<td width="22%" BGCOLOR="#fff"><? echo $rows['enteredby']; ?></td>
</tr>
<?php
}
?>

<tr>
<td colspan="5" align="left" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete Row"></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 "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>

</table>

最佳答案

尝试使用

if(!empty($checkbox)) {
$sql = "DELETE FROM $tbl_name WHERE id IN(".implode(",", $checkbox).")";
$result = mysql_query($sql);
}

关于php - 使用复选框从 mysql 中删除多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22404366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com