1

我对 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);
4

2 回答 2

3

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);
?>
于 2012-11-30T12:27:53.283 回答
0

HTML 代码

<html>
<form method="post" action="somepage">
<input type="text" name="email" />
<br/>
<input type="Submit" name="submit" value="Delete" />
</form>
</html>

php代码

<?php
// on submit button 
if (isset($_POST["submit"]))
{
$dbhost = 'localhost';
$dbuser = 'databaseuser';
$dbpass = 'databasepassword';
$dbname = 'databasename';
//connection to mysql
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
//use database
$usedb = mysql_select_db($dbname , $conn);
if (!$usedb)
{
die ("could not select $dbname...<br/>");
}
//to delete record
$del="DELETE FROM tablename WHERE columnName = '$email'";
if(mysql_query($del))
{
echo '<script type="text/javascript">alert("Record Deleted Successfully....!")</script>';
}
else
{
echo mysql_error();
}
}
?>

希望这对你有帮助。

于 2012-11-30T12:37:00.120 回答