PHP仅在条目为数字时删除行

PHP仅在条目为数字时删除行,php,mysql,sql-delete,Php,Mysql,Sql Delete,此代码设置在灯组上。我的删除页面有问题。PHP中的Delete函数只有在mysql表中的条目是数字时才起作用。我需要它来删除该行,而不考虑条目。在mysql中,电子邮件字段设置为varchar25。我没有在代码中定义变量,所以我不明白为什么它只限于数字 页面如下: <!DOCTYPE HTML> <html> <head> </head> <body> <div id="gradientbackground">

此代码设置在灯组上。我的删除页面有问题。PHP中的Delete函数只有在mysql表中的条目是数字时才起作用。我需要它来删除该行,而不考虑条目。在mysql中,电子邮件字段设置为varchar25。我没有在代码中定义变量,所以我不明白为什么它只限于数字

页面如下:

<!DOCTYPE HTML>
<html>
<head>

</head>
<body>
<div id="gradientbackground">

    <table class="beachpictures" align="center" border="1">
        <thead>

        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Website</th>
            <th>Gender</th>
            <th>Delete</th>
        </tr>
        </thead>
        <tbody>
    <?php

        require 'project_db.php';
        mysql_connect("$servername", "$username", "$password")or die("cannot connect");
        mysql_select_db("$database")or die("cannot select DB");

        //execute the SQL query and return records
        if (!$result = mysql_query("SELECT *  FROM $table"))
        echo 'mysql error: ' .mysql_error();

        //fetch tha data from the database
        while ($row = mysql_fetch_array($result)) {
    ?>

        <tr>
            <td><?php echo $row['Name']; ?></td>
            <td><?php echo $row['Email']; ?></td>
            <td><?php echo $row['Website']; ?></td>
            <td><?php echo $row['Gender']; ?></td>
            <td class="record-delete">
                <form action='delete.php?Email="<?php echo $row['Email']; ?>"' method="post">
                    <input type="hidden" name="Email" value="<?php echo $row['Email']; ?>">
                    <input type="submit" name="submit" value="Delete">
                </form>
            </td>
        </tr>
   <?php }
    ?>
        </tbody>
    </table>
<br>
</div>
</body>
</html>
下面是执行实际查询的delete.php文件:

<?php
    require 'project_db.php';
    mysql_connect("$servername", "$username", "$password")or die("cannot connect");
    mysql_select_db("$database")or die("cannot select DB");

//Define the query
$query = "DELETE FROM $table WHERE Email={$_POST['Email']} LIMIT 1";

//sends the query to delete the entry
mysql_query ($query);

if (mysql_affected_rows() > 0) {
//if it updated
?>
            <strong>Record Has Been Deleted</strong><br /><br />
<?php
 } else {
//if it failed
?>
            <strong>Deletion Failed</strong><br /><br />
<?php
}


?>

您需要添加引号:否则,mysql将期望$u POST['email']是一个数字。 另外,您应该切换到或,因为mysql_*函数已被弃用,并且您已向打开。 因此,您的代码应该是:

<?php
    require 'project_db.php';
    $connect = mysqli_connect($servername,$username,$password,$database)or die("cannot connect");

//Define the query
$query = "DELETE FROM $table WHERE Email='".mysqli_real_escape_string($connect,$_POST['Email'])."' LIMIT 1";

//sends the query to delete the entry
$execute = mysqli_query ($connect,$query);

if (mysqli_affected_rows($execute) > 0) {
//if it updated
?>
            <strong>Record Has Been Deleted</strong><br /><br />
<?php
 } else {
//if it failed
?>
            <strong>Deletion Failed</strong><br /><br />
<?php
}


?>

这很有效!!非常感谢。唯一剩下的问题是,即使它实际上成功了,它还是失败了。@Noor不客气:这是我的错,对不起,我更新了我的答案,现在它应该可以工作了谢谢@Stude,它没有修复它,但后来我颠倒了两条语句,它工作得很好-非常感谢@不,我很高兴你解决了: