我有一个PHP,我无法让它工作。它的功能是连接到我的数据库并从数据库中删除输入的电子邮件地址。但由于某种原因,结果始终是“数据未删除”。我确信所有的细节,如数据库连接细节和列名称等仍然代码不起作用。
代码:
<?php
// php code to Delete data from mysql database
if(isset($_POST['delete']))
{
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "newsletter";
// get id to delete
$email = $_POST['email'];
// connect to mysql
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
// mysql delete query
$query = "DELETE FROM `email_user` WHERE `email` = $email";
$result = mysqli_query($connect, $query);
if($result)
{
echo 'Data Deleted';
}else{
echo 'Data Not Deleted';
}
mysqli_close($connect);
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP DELETE DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="unsub.php" method="post">
ID TO DELETE: <input type="text" name="email" required><br><br>
<input type="submit" name="delete" value="Clear Data">
</form>
</body>
</html>
请指出我在这里做错了什么?
由于
答案 0 :(得分:2)
您需要在查询中添加$email
周围的引号,因为它是一个字符串。
$query = "DELETE FROM `email_user` WHERE `email` = '$email'";
注意: - 您的代码对 SQL INJECTION 是开放的。您必须使用 prepared statements
< /强>
帮助参考: -
答案 1 :(得分:0)
尝试将查询修改为以下内容:
"DELETE FROM `email_user` WHERE `email` = '$email'"