您好我正在尝试创建一个小的电子邮件订阅者脚本,用于向我的数据库添加电子邮件地址。我能够使用像mysql_connect这样的弃用函数使其工作正常但是使用mysqli更好。但由于某种原因,我无法再将电子邮件地址插入数据库,但我可以正常连接到数据库,并检查电子邮件地址是否已经存在。我想知道为什么我的INSERT似乎不起作用。我没有多少PHP使用经验。
if (!$link) {
echo "save_failed cannot connect"; //if cant connect show error
return;
}
mysqli_select_db($link,$mydb);
// Clean variables before performing insert
$clean_email = mysqli_real_escape_string($link,$_POST['email']);
$clean_subscriber = mysqli_real_escape_string($link,$_POST['firstname']);
$clean_date = mysqli_real_escape_string($link,$_POST['date']);
$query = "SELECT * FROM EmailList WHERE email = '{$email}'";//check if already in list
$result = mysqli_query($link,$query);
$row_cnt = mysqli_num_rows($result);
if($row_cnt == 0) {
// Perform insert if not in list
$sql = "INSERT INTO EmailList (Email,Name,Date) VALUES
('$clean_email','$clean_subscriber','$clean_date')";
echo "Thank you for Subscribing to my blog!";
} else {
echo "You have subscribed already. Thank you for subscribing";
}
答案 0 :(得分:2)
看起来你错过了运行查询的行:
mysqli_query($link,$sql);
答案 1 :(得分:2)
您需要使用
执行查询像这样:mysqli_query()
if (!$link) {
echo "save_failed cannot connect"; //if cant connect show error
return;
}
mysqli_select_db($link,$mydb);
// Clean variables before performing insert
$clean_email = mysqli_real_escape_string($link,$_POST['email']);
$clean_subscriber = mysqli_real_escape_string($link,$_POST['firstname']);
$clean_date = mysqli_real_escape_string($link,$_POST['date']);
$query = "SELECT * FROM EmailList WHERE email = '{$email}'";//check if already in list
$result = mysqli_query($link,$query);
$row_cnt = mysqli_num_rows($result);
if($row_cnt == 0) {
// Perform insert if not in list
$sql = "INSERT INTO EmailList (Email,Name,Date) VALUES
('$clean_email','$clean_subscriber','$clean_date')";
mysqli_query($link,$sql)
echo "Thank you for Subscribing to my blog!";
} else {
echo "You have subscribed already. Thank you for subscribing";
}