我得到了一个表单输入字段,用户在其中写下电子邮件以订阅时事通讯。我必须验证电子邮件,并检查它是否已在我的数据库中。现在所有检查都是正确的,但是当它实际上需要将电子邮件插入我的数据库时,它会在我的 sql 语句上给我一个语法错误。多次检查,调整它,但我找不到问题。我什至只是在我的PhPMyAdmin中添加了一行,并复制粘贴了执行的代码。还是没有。因此,如果我对此进行测试,它会验证电子邮件,然后我尝试了一封已经在我的数据库中的电子邮件,它正确地给出了它已经在数据库中的输出,但是当它必须转到最后一个时,它会给我和错误:
您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行"email@email.com"附近使用的正确语法
我的代码如下:
<form method="post" action="#anker" class="footerform" id="anker">
<input type="email" value="" name="fldEmail" class="brief" id="nieuwsbrief" placeholder="Nieuwsbrief Email" >
<input type="submit" value="Schrijf me in!" name="subscribe" id="nieuwsbriefsubmit" class="btn"></div>
<?php
require('db.php');
if (isset($_POST["subscribe"])) {
if ($_POST["fldEmail"]=="") {
echo "<p>error: Vul het veld in aub</p>";
}else {
$fldEmail=$_POST['fldEmail'];
$fldEmail =filter_var($fldEmail, FILTER_SANITIZE_EMAIL);
$fldEmail= filter_var($fldEmail, FILTER_VALIDATE_EMAIL);
if (!$fldEmail) {
echo "<p>Je email-adres is niet correct</p>";
} else {
$fldEmail = stripslashes($fldEmail);
$fldEmail = mysqli_real_escape_string($connection, $fldEmail);
$testquery = "SELECT fldEmail FROM nieuwsbrief WHERE fldEmail = '$fldEmail'";
$result = mysqli_query($connection, $testquery) or die(mysqli_error($connection));
$rows = mysqli_num_rows($result);
if ($rows > 0) {
echo "emailadres zit al in onze databank";
} else {
$query = "INSERT INTO nieuwsbrief (fldEmail) VALUES '$fldEmail'";
$result2 = mysqli_query($connection, $query) or die(mysqli_error($connection));
echo "<p>Bedankt om je op de nieuwsbrief in te schrijven</p>";
}
}
}
}
?>
</form>
SQL Sintax 不正确
用
$query = "INSERT INTO nieuwsbrief (fldEmail) VALUES ('$fldEmail') ";
检查这个