-1

我有一个 PHP 脚本,它使用 mysqli 扩展名连接到 MySQL 数据库,以根据用户名或 ID 搜索博客文章。我创建了一个名为 VIEW 的视图BlogSearch,它使用来自其他表的连接来聚合我需要的信息,如下所示:

它从中提取的表被称为Profiles具有用户信息的表,BlogPosts并且BlogCategory

每次我搜索我都会得到错误: Unknown column 'chenzhen' in 'where clause'

我在下面使用的 PHP 代码:

require 'database.php'; 

        $query = "SELECT * FROM BlogSearch";

  echo <<<EOF
<form method='post' action='' style="padding: 30px 0;">
        <table cellspacing="0" border="0" style="float: left;">
        <tr>
        <td>Search Blog Posts by Username/ID</td>
        <td><input type="text" id="search" name="search" style="width: 300px;"/></td>
        <td><input type="submit" id="submit_button" value="Search" name="submit_button" style="float: right;" /></td>
        </tr>
        </table>
    </form>
EOF;

        if(isset($_POST['submit_button'])) 
    {
        $search_term = $_POST['search'];
          $query = $query . " WHERE `NickName` LIKE '%$search_term%' OR ID = $search_term ";


        // run the query and store the results in the $result variable.
        $result = $mysqli->query($query) or die(mysqli_error($mysqli));

    }

    if ($result) {

  // create a new form and then put the results
  // into a table.



  echo "<form method='post' action='delete.php' style='clear: both;'>"; 
  echo "<table cellspacing='0' cellpadding='15'>
        <th width='5%'>
        <input type='checkbox' id='allcb' onclick='checkAll(this)' name='allcb' />Check All
        </th>
        <th width='10%'>User</th>
        <th width='85%'>Blog Post Title</th>
        ";


  while ($row = $result->fetch_object()) {

        $title = substr($row->PostCaption,0,50);
        $id = $row->PostID;
        $user = $row->NickName;

        //put each record into a new table row with a checkbox
    echo "<tr>
            <td><input type='checkbox' name='checkbox[]' id='checkbox[]'  value=$id />
            <td>$user</td>
            <td>$title</td>
         </tr>"; 

    }

    // when the loop is complete, close off the list.
    echo "</table><p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p></form>";
}

我不知道为什么它甚至将用户名标识为列。谁能指出我正确的方向来解决这个问题?

提前致谢。

4

2 回答 2

1

SQL 查询中任何不是 SQL 关键字或文字(用单引号表示)的元素都被假定为对象(例如表、列)名称。

$search_term您的问题是您的条款中缺少引号WHERE

$query = $query . " WHERE `NickName` LIKE '%$search_term%' OR ID = $search_term ";

您应该添加它们,如下所示:

$query = $query . " WHERE `NickName` LIKE '%$search_term%' OR ID = '$search_term' ";
于 2013-10-19T19:12:25.443 回答
1

在这样的 where 子句中用单引号将您的$search_term括起来'$search_term'

于 2013-10-19T19:13:45.520 回答