MYSQL& PHP没有问题连接数据库但在发布到数据库时出现问题

时间:2015-10-20 04:11:48

标签: php mysql

$sql = "INSERT INTO 'testdatabase`.`newuserformtable' (`First Name`,`Last Name`, `Title`)
VALUES ('John', 'Doe', 'john@example.com')";

 if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

我已尝试过所有内容,但仍然收到此错误消息:

Connected successfullyError: INSERT INTO 'testdatabase`.`newuserformtable`(`First Name`, `Last Name`, `Title`) VALUES ('John', 'Doe', 'john@example.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 ''testdatabase`.`newuserformtable' (`First Name`, `Last Name`, `Title`) VALUES ' at line 1 

6 个答案:

答案 0 :(得分:1)

问题在于您的查询中的单引号(&#39;)和反向标记(`)。尝试使用下面的内容,我无法将其置于评论中

INSERT INTO `testdatabase`.`newuserformtable` ...

答案 1 :(得分:1)

不应将数据库名称或表名视为字符串。

所以sql应该是:

<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

答案 2 :(得分:0)

混合的单引号似乎有误
$sql = "INSERT INTO `testdatabase`.`newuserformtable' (`First Name`,`Last Name`, `Title`)
VALUES ('John', 'Doe', 'john@example.com')";

答案 3 :(得分:0)

$ sql =&#34; INSERT INTO testdatabasenewuserformtable' (名字,姓氏,标题`) 价值观(&#39; John&#39;,&#39; Doe&#39;,&#39; john@example.com')&#34 ;;

用你的引号写这个问题.....还有checkdatatype和length 如果您的错误发布了其他错误

答案 4 :(得分:0)

您在数据库和表名上使用了单引号(')而不是反引号(`)。

$sql = "INSERT INTO `testdatabase`.`newuserformtable`
            (`First Name`,`Last Name`, `Title`)
        VALUES
            ('John', 'Doe', 'john@example.com')";

答案 5 :(得分:0)

$sql = "INSERT INTO `testdatabase`.`newuserformtable'
       (`First Name`,`Last Name`, `Title`)
       VALUES 
       ('John', 'Doe', 'john@example.com')";

$res = mysql_query($sql);

if ($res) {
    echo "New record created successfully";
} 
else {
    echo "Error";
}