单击按钮两次后代码工作

时间:2014-03-24 05:44:47

标签: php mysql

我正在使用以下代码从数据库中删除多个虚拟对象,但有些我必须单击删除按钮两次以删除值,请查看以下代码并建议我如何通过单击删除值

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

4 个答案:

答案 0 :(得分:0)

只需将您的选择查询移至删除代码下方或使用此

即可
<?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);
?>

答案 1 :(得分:0)

您需要在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);

注意: mysql_ *函数已弃用,因此请使用mysqli_*PDO

答案 2 :(得分:0)

您必须重新排列代码,以便在删除

后从db获取数据
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注入漏洞

How can I prevent SQL injection in PHP?

答案 3 :(得分:0)

我首先将你的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>