我对PHP有点陌生,但目前我正在做一门基于它的课程。
我有一个表单,用户将输入他们的电子邮件地址,以便将其从电子邮件列表中删除。
我试图做一个if else声明,当他们的电子邮件不在数据库中时,它会出错,“不存在于列表中”。
正如你可能看到的脚本不起作用。
如何查看查询是否成功,然后向用户返回适当的消息?

    $email = $_POST['email'];

    $dbc = mysqli_connect(censored)
            or die('error connecting to the database');
    $query = "DELETE FROM email_list WHERE email='$email'";
    $result = mysqli_query($dbc, $query)
            or die('Error Querying the database');

    if ($result == 0) {

        echo $email . ' do not exist in the list';
    }
    else {
        echo $email . ' is deleted form the list';
    }

    mysqli_close($dbc);

最佳答案

mysqli_affected_rows()

<?php
$email = $_POST['email'];

$dbc = mysqli_connect(censored)
        or die('error connecting to the database');
$query = "DELETE FROM email_list WHERE email='$email'";
$result = mysqli_query($dbc, $query)
        or die('Error Querying the database');

if (mysqli_affected_rows($dbc) === 0) {

    echo $email . ' do not exist in the list';
}
else {
    echo $email . ' is deleted form the list';
}

mysqli_close($dbc);
?>

10-04 10:44
查看更多