2014-01-28 49 views
0

這就是我如何創建search option does not appear on the page這個線程和罰款我必須工作,但這是我應該如何查找表中的3個領域,它以任何方式工作。同時搜索三個區域,

if($stmt = $this->mysqli->prepare('SELECT `navn`, `link`, `img`, `omrade` FROM `ordblindtest` WHERE `navn` LIKE "%" . $navn . "%" OR `omrade` LIKE "%" . $omrade . "%" OR `sogord` LIKE "%" . $sogord . "%"')) 
    { 
     $stmt->bind_param('sss', $navn, $omrade, $sogord); 
     $navn = $_POST["sogord"]; 
     $omrade = $_POST["sogord"]; 
     $sogord = $_POST["sogord"]; 
     ... 
    } 

問題是,我得到這個錯誤:

Error on: 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 '. $navn . "%", OR omrade LIKE "%" . $omrade . "%", OR sogord LIKE "%" . $sog' at line 1

我不明白爲什麼它似乎有錯誤。爲什麼我收到錯誤?

+0

你注入的變量到你的查詢。這不是如何使用準備好的語句。 – h2ooooooo

+0

[搜索選項沒有出現在頁面上]可能的重複(http://stackoverflow.com/questions/21407213/search-option-does-not-appear-on-the-page) – MichaelRushton

回答

1

因爲你使用準備好的語句,你應該使用?佔位符代替的變量,所以您的查詢應該是這樣的:

if ($stmt = $this->mysqli->prepare(' 

    SELECT 
    `navn`, 
    `link`, 
    `img`, 
    `omrade` 
    FROM 
    `ordblindtest` 
    WHERE 
    `navn` LIKE CONCAT("%", ?, "%") 
    OR 
    `omrade` LIKE CONCAT("%", ?, "%") 
    OR 
    `sogord` LIKE CONCAT("%", ?, "%") 

')) 
{ 

    $stmt->bind_param('sss', $navn, $omrade, $sogord); 

    // ... 
+0

感謝您的幫助Michael。 :d – Jesperbook