SQL语法中的错误

时间:2015-08-18 10:45:14

标签: php

错误是:

  

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在' =''或者作者='''''在第1行

我的PHP代码:

<?php
include "search.php";

$sql = "SELECT * FROM bookinfo";

if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['searchbox']);

$sql .= "WHERE title= '{$search_term}'";
$sql .= "OR author= '{$search_term}'";
}

$query = mysql_query($sql) or die(mysql_error());

?>
<form name="searchform" method="POST" >
Search:<input type="text" name="searchbox" />
<input type="submit" name="search" value="search book" />
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
    <td>Title</td>
    <td>Author</td>
    <td>Price</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) {
?>   
<tr>
    <td><?php echo $row['title']; ?></td>
    <td><?php echo $row['author']; ?></td>
    <td><?php echo $row['price']; ?></td>
</tr>
<?php }
?>

</table>

3 个答案:

答案 0 :(得分:4)

ORWHERE之前添加空格。目前它正在产生类似 -

的东西
SELECT * FROM bookinfoWHERE title= 'hello'OR author= 'Hello'

应该是 -

$sql .= " WHERE title= '{$search_term}'";
$sql .= " OR author= '{$search_term}'";

答案 1 :(得分:1)

我认为你在OR之前错过了空间:

$sql .= " WHERE title= '{$search_term}'";
$sql .= " OR author= '{$search_term}'";

答案 2 :(得分:1)

您需要将space放在两个附加语句之前:

$sql .= " WHERE title= '{$search_term}'";
$sql .= " OR author= '{$search_term}'";