PHP和mysql语法错误

标签 php mysql syntax

<分区>

我的 register.php 不会查询最简单的查询:

<?php
session_start();

/* Connect and Query database "accounts", then close connection */
$connection=mysql_connect("localhost","root","");
if (!$connection)
{
    die('Cannot connect to MySQL. Error: ' . mysql_error());
}

$check_database = mysql_select_db("accounts", $connection);
if (!$check_database)
{
    die('Cannot connect to database. Error: ' . mysql_error());
}

/* Query database to save user's post */
/* If field "repostid=0", then the post is not a repost; if the field "repostid>0", then the post is a repost with the field linking to the id of the post to be reposted */ 
$result = mysql_query("INSERT INTO posts2 (from) VALUES ('h1')");
if (!$result)
{
    die('Cannot query. Error: ' . mysql_error());
}

/* Close Connection */
mysql_close($connection);

/* Redirect to user's page */
header("Location: /website/index.php", 404);
exit();

?>

这是回显的错误信息:

Cannot query. Error: 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 'from) VALUES ('h1')' at line 1

它从 post.php 上的表单调用:

    <form name="postForm" action="userpost.php" method="post" enctype="multipart/form-data" onsubmit="validatePostForm()">
        <div id="posttext"><p>Enter the text you wish to post, then press "Post":</p></div>
        <div id="posttextarea"><textarea rows="10" cols="80" name="postinfo" onkeydown="characterTyping('postbuttoncharactersleft')"></textarea></div>
        <div id="postbutton"><input type="submit" value="Post"/></div><p id="postbuttoncharactersleft">250</p><p id="postbuttoncharacterslefttext"> characters left.</p>
    </form>

这是数据库表,因此您可以确认它是查询此表的正确语法:

enter image description here

最佳答案

你需要转义reserved words in MySQL像带有反引号的 from

INSERT INTO posts2 (`from`) VALUES ('h1')

关于PHP和mysql语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19777532/

相关文章:

javascript - 基于 HTML 的小册子/传单

php - Wordpress 子主题 style.css 不工作

MySQL 查询 -> 在子表中查找具有两个条件的记录

php - 无法弄清楚为什么 $wpdb 在 WordPress 页面中不起作用

Python:从子类更改父类属性

php - QuickBooks webhooks 在本地与 ngrok 一起工作,但不能在生产中工作

php - 保护 WordPress 中的插件数据免遭其他插件访问?

java - liferay 的 service.xml 中的 Blob 数据类型

powershell - PowerShell格式化Cmdlet输出

syntax - powershell中的条件执行(&&和||)