Php 点击两次按钮后代码工作

Php 点击两次按钮后代码工作,php,mysql,Php,Mysql,我使用下面的代码从数据库中删除多个VLUE,但是我必须单击两次删除按钮才能删除值,请查看下面的代码,并建议我如何通过单击删除值 <?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

我使用下面的代码从数据库中删除多个VLUE,但是我必须单击两次删除按钮才能删除值,请查看下面的代码,并建议我如何通过单击删除值

<?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>

只需将select查询移动到“删除代码”下方或使用此选项即可

<?php
include("connection.php");
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); 
                }
            }

    }
}
$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>

您需要将以下代码移动到includeconnection.php之后的位置;线

在这个地方之后是代码

$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

注意:mysql_*函数已被弃用,因此,使用或

时,您必须重新排列代码,以便它在删除后从数据库中获取数据

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); 
                }
            }

    }
}

$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

PS:请考虑SQL注入漏洞


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

connection.php:

您的删除文件:


人们已经回答了你的问题,但我强烈建议你停止使用mysql函数,mysqli函数已经被弃用了——在你重构时,我强烈建议你更新你的数据库连接方法
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); 
                }
            }

    }
}

$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
<?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>