在PHP中连续删除记录

时间:2015-07-08 05:56:03

标签: php html mysql

我试图删除数据库中的记录。所以基本上我创建了一个包含所有记录的表。现在我需要做的是当我点击“删除”链接时,它将删除记录选择的行。

这是它的样子:

所以基本上我这里有3页。
1. page.php
2. add.php
3. delete.php

这是我的page.php文件:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#2196F3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.Toolbar>

这是我的add.php文件:

<table border="1">
    <thead>
        <th>email</th>
        <th>date</th>
        <th>delete</th>
    </thead>

    <tbody>
        <tr>
        <?php 
            foreach($emails as $mail){ ?>
            <td><?php echo $mail['email']; ?></td>
            <td><?php echo $mail['date']; ?></td>
            <td><?php echo "<a href='delete.php?id=". $mail['id']. "'>DELETE</a>"; ?></td>
        </tr>
            <?php } ?>


    </tbody>
</table>

到目前为止,这是我的delete.php文件:

<?php
    require("new-connection.php");

    session_start();

    $email = $_POST['email'];
    if(empty($_POST['email']) AND (filter_var($email, FILTER_VALIDATE_EMAIL) === false))
     {
          $_SESSION['message'] = "email cannot be blank";
     }else{
        $query = "INSERT INTO email_tbl (email, date)
              VALUES('$email', NOW())";       

    $insertEmail = run_mysql_query($query);


    if(run_mysql_query($query))
    {
        $_SESSION['message'] .= "New RECORD has been added correctly!";
    }
    else
    {
        $_SESSION['message'] .= "Failed to add new Interest"; 
    }

    }


    header('Location: email.php');




?>

所以现在当我点击删除链接时,它必须实时删除按钮。任何的想法?

4 个答案:

答案 0 :(得分:6)

修改delete.php以检索url参数:

<?php
    require("new-connection.php");
    session_start();

    $id = $_GET['id'];

    $query = "DELETE FROM email_tbl
            WHERE id='$id' LIMIT 1";       

    $deleteEmail = run_mysql_query($query);


    if($deleteEmail)
    {
        $_SESSION['message'] .= "RECORD has been DELETED correctly!";
    }
    else
    {
        $_SESSION['message'] .= "Failed to DELETE RECORD"; 
    }

    header('Location: email.php');
?>

至于你的add.php,你使用的是:

$insertEmail = run_mysql_query($query);

    if(run_mysql_query($query))

您应该将其更改为:

$insertEmail = run_mysql_query($query);


    if($insertEmail)

您现在正在做的是通过两次调用run_mysql_query来执行查询两次。这应该解决它

答案 1 :(得分:1)

$query = "DELETE FROM email_tbl WHERE id=".$_GET['id']." LIMIT 1";

答案 2 :(得分:1)

更新delete.php文件:

$id = $_GET['id'];

$query = "DELETE FROM email_tbl WHERE id='$id' LIMIT 1";  

并查看以下部分: 您正在运行查询两次。所以很明显它会两次添加相同的记录。

$insertEmail = run_mysql_query($query);

if(run_mysql_query($query))
{
    $_SESSION['message'] .= "New RECORD has been added correctly!";
}

修改代码以仅使用run_mysql_query一次。

答案 3 :(得分:1)

当php中的run_mysql_query函数有什么意义?

http://php.net/manual-lookup.php?pattern=run_mysql_query&scope=quickref