gpt4 book ai didi

php - 单击按钮两次后代码可以工作

转载 作者:行者123 更新时间:2023-11-30 00:29:43 24 4
gpt4 key购买 nike

我正在使用以下代码从数据库中删除多个值,但有些我必须单击删除按钮两次才能删除值,请仔细阅读以下代码并建议我如何通过单击删除值

<?php

include("connection.php");
$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if (isset($_POST['delete']))
{
if (isset($_POST['checkbox']))
{
$checkbox = $_POST['checkbox'];
if (is_array($checkbox)) {

foreach ($checkbox as $key => $id)
{
mysql_query("DELETE FROM deptag WHERE id=".$id);
}
}

}
}
?>
<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>Tag</strong></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name='checkbox[]' type='checkbox' id='checkbox' value="<?php echo $rows['id']; ?>"></td>

<td bgcolor="#FFFFFF"><?php echo $rows['tagdep']; ?></td>

</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

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

最佳答案

我将首先将您的 MySQL 转换为 MySQLi:

connection.php:

<?php

$con=mysqli_connect("localhost","YourUsername","YourPassword","YourDatabase");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

?>

您的删除文件:

<?php

include("connection.php");

if (isset($_POST['delete']))
{
$counter=$_POST['hiddencounter'];
$checkbox=$_POST['checkbox'];

for($x=0;$x<=$counter;$x++){

if(empty($checkbox[$x])){
/* DO NOTHING */
}

else {
mysqli_query($con,"DELETE FROM deptag WHERE id='checkbox[$x]'");
}

} /* END OF FOR LOOP */

} /* END OF IF ISSET DELETE */

$result=mysqli_query($con,"SELECT * FROM deptag");
$count=mysqli_num_rows($result);

?>
<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>Tag</strong></td>

</tr>
<?php

$counter=0; /* LET US SET A COUNTER */

while($rows=mysqli_fetch_array($result)){
$id=$rows['id'];
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><?php echo "<input name='checkbox[$counter]' type='checkbox' id='checkbox' value='$id'>"; ?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['tagdep']; ?></td>

</tr>
<?php
$counter=$counter+1; /* INCREMENT COUNTER EVERY LOOP */
} /* END OF WHILE LOOP */
echo "<input type='hidden' name='hiddencounter' value='$counter'>"; /* PUT THE OVERALL COUNTER INTO A HIDDEN FIELD */
?>
<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 - 单击按钮两次后代码可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22601913/

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