2013-11-04 41 views
0

register.php不會查詢的查詢簡單:PHP和MySQL的語法錯誤

<?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

回答

6

你需要逃避reserved words in MySQLfrom與反引號

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

我其實打了一個保留字?有什麼機會......我覺得很愚蠢。謝謝! –

+0

@shawna以供參考:http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html –

0

添加;在SQL語句的結束以及PHP的線的末端,這樣

$result = mysql_query("INSERT INTO posts2 (`from`) VALUES ('h1');"); 

這裏是SQL插入的例子:

INSERT INTO客戶(客戶名稱,城市,國家)VALUES( '紅衣主教','斯塔萬格','挪威');

+0

HAHA,甚至沒有注意到他的背部剔除,我只是把它們放在我的,因爲從...好趕上juergen d – SecureLive

+0

你也不必把分號放在查詢那樣,它沒有它的工作 –