我陷入了SQL查询将数据插入表的语法错误。 啊,语法错误,所有错误中最没用的!!
使用同时使用mysqli和PDO方法从PHP Insert Data Into MySQL修改的代码。 例如:
<?php
$servername = "localhost";
$username = "4w_write";
$password = "GjByhJzrQueHgTzw";
$dbname = "4w_test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO 4w (email) VALUES ($email)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
已将其简化为不使用关键字的单个变量,我很确定问题出在表上。
SQL查询:
INSERT INTO 4w (email) VALUES (myemail@gmail.com)
错误:
错误:INSERT INTO 4w(email)VALUES(myemail@gmail.com)您有一个 您的SQL语法错误;检查与您的手册相对应的手册 MariaDB服务器版本可在'@ gmail.com)附近使用正确的语法 在第1行
SQL表(4w):
# Name Type Default
1 id [Primary,Index] int(11)
2 email varchar(255)
3 whatIs tinytext
4 whereIs text
5 whattodo text
6 imageURL text
7 whenRep timestamp CURRENT_TIMESTAMP
答案 0 :(得分:1)
电子邮件值是一个字符串,因此您需要用引号将其括起来:
$sql = "INSERT INTO 4w (email) VALUES ('$email')";
或者更好的是,使用准备好的语句并绑定其值。
答案 1 :(得分:0)
Couldn't see the problem, missing quotes.... Original code:
$sql = "INSERT INTO 4w (email) VALUES ($email)";
Fixed code:
$sql = "INSERT INTO 4w (email) VALUES ('$email')";