其中子句中的未知列“陈珍”


Unknown column 'chenzhen' in where clause

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

它从中提取的表称为具有用户信息、BlogPostsBlogCategoryProfiles

每次搜索时都会收到错误: 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>";
}

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

提前谢谢。

SQL

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

您的问题是WHERE子句中$search_term周围缺少引号:

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

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

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

你的 $search_term 括在单引号中,就像这样'$search_term'