简体   繁体   English

“搜索数据库列时,”Where子句中的未知列'Bloomsbury'错误

[英]"Unknown column 'Bloomsbury in the Where Clause' Error when searching database column

After Selection from drop down list 从下拉列表中选择后

PHP CODE To Create Dropdown List PHP代码创建下拉列表

function getPublishers (){

    $sql = "SELECT DISTINCT bookid, publisher FROM book GROUP BY publisher ORDER BY    
publisher ASC";
    $rs = mysql_query($sql) or die(mysql_error());
    $rows = mysql_fetch_assoc($rs);
    $tot_rows = mysql_num_rows($rs);
    if($tot_rows>0){
        echo "<select name=\"srch_publisher\" id=\"srch_publisher\">\n";
        echo "<option value=\"\">Any Publisher&hellip;</option>\n";
        do{
            echo "<option value=\"".$rows['bookid']."\"";
            getSticky(2, 'srch_publisher', $rows['bookid']);
            echo ">".$rows['publisher']."</option>";
        } while($rows = mysql_fetch_assoc($rs));
        echo "</select>";
    }
    mysql_free_result($rs);
}

PHP CODE that carries out query based on selection 基于选择执行查询的PHP CODE

$sql =  "SELECT DISTINCT bk.title AS Title, bk.year AS Year, bk.publisher AS Publisher, aut.authorname AS Author 
         FROM book bk 

         JOIN book_category bk_cat 
         ON bk_cat.book_id = bk.bookid

         JOIN categories cat 
         ON cat.id = bk_cat.category_id

         JOIN books_authors bk_aut 
         ON bk_aut.book_id = bk.bookid

         JOIN authors aut
         ON aut.id = bk_aut.author_id";

if(isset($_GET['searchInput'])){
$input = $_GET['searchInput'];
$input = preg_replace('/[^A-Za-z0-9]/', '', $input);
}
if (isset($input)){

    $getters = array();
    $queries = array();

    foreach ($_GET as $key => $value) {
        $temp = is_array($value) ? $value : trim($value);
        if (!empty($temp)){
        if (!in_array($key, $getters)){
            $getters[$key] = $value;
            }
        }
    }

    if (!empty($getters)) {

        foreach($getters as $key => $value){
            ${$key} = $value;
            switch ($key) {
                case 'searchInput':
                    array_push($queries,"(bk.title LIKE '%$searchInput%' 
                    || bk.description LIKE '%$searchInput%' || bk.isbn LIKE '%$searchInput%' 
                    || bk.keywords LIKE '%$searchInput%' || aut.authorname LIKE '%$searchInput%')");
                break;
                case 'srch_publisher':
                    array_push($queries, "(bk.bookid = $srch_publisher)");
                break;
                case 'srch_author':
                    array_push($queries, "(bk_aut.author_id = $srch_author)");
                break;          
        }
    }
}

if(!empty($queries)){
    $sql .= " WHERE ";
    $i = 1;
    foreach ($queries as $query) {
        if($i < count($queries)){
            $sql .= $query." AND ";
        } else {
            $sql .= $query;
        }   
        $i++;
    }
}
$sql .= " GROUP BY bk.title  ORDER BY bk.title ASC";

}else{
    $sql .= " GROUP BY bk.title ORDER BY bk.title ASC";
}

When i search for books with the publisher Bloomsbury or any other publisher i get "Unknown column 'Bloomsbury in the Where Clause' even tho i am searching the column of publisher 当我与出版商Bloomsbury或任何其他出版商搜索书籍时,我在“Where Where Clause”中获得“Unknown column'Bloomsbury',即使我正在搜索出版商专栏

The line 这条线

array_push($queries, "(bk.bookid = $srch_publisher)"

Should be 应该

array_push($queries, "(bk.bookid = '$srch_publisher')"

...you need quotes around strings if you're going to search on them. ...如果您要搜索字符串,则需要在字符串周围添加引号。

HOWEVER - you shouldn't introduce parameters in this way, and the mysql_* functions are deprecated. 但是 - 您不应该以这种方式引入参数,并且不推荐使用mysql_ *函数。 Have a look at mysqli ( http://uk3.php.net/manual/en/book.mysqli.php ) or PDO ( http://uk3.php.net/manual/en/book.pdo.php ) and parameter binding. 看看mysqli( http://uk3.php.net/manual/en/book.mysqli.php )或PDO( http://uk3.php.net/manual/en/book.pdo.php )和参数绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM