2016-07-31 94 views
0
$sql="SELECT * FROM book_seat INNER JOIN seat ON book_seat.seat_id=seat.seat_id 
           INNER JOIN users ON book_seat.user_id=users.user_id 
           WHERE 1=1"; 
      if($type) 
      { 
       $sql.= " AND seat.type=$type";  
      } 

      if($name) 
      { 
       $sql.= " AND users.name=$name";  
      } 

      if($flightdate) 
      { 
       $sql.= " AND book_seat.date_booked=$flightdate"; 
      } 

      if($seat) 
      { 
       $sql.=" AND seat.seat=$seat"; 
      } 

一切都確定了$type$flightdate$seat但如果我輸入$name,它給error.e.g如果我輸入ABC,它提供了以下。用戶表中有一列nameMySQL錯誤未知列「ABC」

異常(數據庫異常) 'YII \分貝\異常' 與消息 「SQLSTATE [42S22]:柱未找到:1054未知列 '' 在 '其中正在執行條款' 的SQL ABC是:SELECT * FROM book_seat INNER JOIN座椅ON book_seat.seat_id = seat.seat_id INNER JOIN用戶ON book_seat.user_id = users.user_id WHERE 1 = 1 AND users.name = ABC」

+2

將名稱變量放在單引號中,因爲它是一個字符串 –

+1

在用戶表中有''AND \'''''''''''''''''''' – Riad

+0

' name'? –

回答

0

嘗試此

AND `users`.`name`='$name'