好的,我到处都是。首先,我有一个表单,一个简报,用户输入他们的电子邮件。一旦他们点击提交,它会检查一个php表单,该表单接收该电子邮件并将其输入到存储电子邮件的SQL数据库中。问题是我在提交表单时遇到此错误
Error: INSERT INTO emails (email) VALUES (random@yahoo.com)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@yahoo.com)' at line 2
不知道为什么它不会把它放入数据库。表单代码是
<section>
<div class="mockup-content">
<div class="morph-button morph-button-inflow morph-button-inflow-1">
<button type="button"><span>Subscribe to our Newsletter</span></button>
<div class="morph-content">
<div>
<div class="content-style-form content-style-form-4">
<h2 class="morph-clone">Subscribe to our Newsletter</h2>
<form action="scripts/mailer.php" method="post">
<p><input type="text" class="button" id="email" name="email" placeholder="NAME@EXAMPLE.COM">
<div>We promise, we won't send you any spam. Just love.</div>
<input type="submit" class="button" id="submit" value="SIGN UP"></p>
</form>
</div>
</div>
</div>
</div><!-- morph-button -->
</div>
</section>
这只是表单的html数据,php数据是
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysql";
$email = ($_POST["email"]);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO emails (email)
VALUES ($email)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
有人可以解释为什么会这样吗?
答案 0 :(得分:1)
在查询中绑定变量时,应使用勾号('
):
$sql = "INSERT INTO emails (email) VALUES ('$email')";
当您尝试绑定的变量值为int
时,可以不使用tick。
答案 1 :(得分:0)
电子邮件是varchar数据类型。你需要在上面加上引号
$sql = "INSERT INTO emails (email) VALUES ('".$email."')";