2017-02-10 124 views
-1

我有以下類方法的MySQL/PHP的插入查詢問題

public function index() 
    { 
     $username = ''; 
     $email = ''; 
     $text = ''; 
     $picture = ''; 

     if (isset($_POST['submit'])) 
     { 
      $username = $_POST['username']; 
      $email = $_POST['email']; 
      $text = $_POST['text']; 
      $picture = $_POST['picture']; 
      Task::create($username, $email, $text, $picture); 
     } 

     require_once(ROOT.'/views/site/index.php'); 
    } 

我的形式發送值,我var_dump($_POST['username'])

這裏檢查是create()方法

public static function create($username, $email, $text, $picture='1', $check_token=false) 
    { 
     $db = Db::connect(); 
     $query = 'INSERT INTO tasks (username, email, text, picture, check_token) 
       VALUES (:username, :email, :text, :picture, :check_token)'; 

     $result = $db->prepare($query); 
     $result->bindParam(':username', $username, PDO::PARAM_STR); 
     $result->bindParam(':email', $email, PDO::PARAM_STR); 
     $result->bindParam(':text', $text, PDO::PARAM_STR); 
     $result->bindParam(':picture', $picture, PDO::PARAM_STR); 
     $result->bindParam(':check_token', $check_token, PDO::PARAM_STR); 

     $result->execute(); 
    } 

我的數據庫連接正確導致我的認證系統起作用。但是這個查詢不會插入任何東西。我的代碼出了什麼問題? 感謝您的幫助。

+0

任何錯誤返回或一切順利嗎? –

+0

'stmt-> errorInfo()'函數 –

+0

@HonzaRydrych有這樣的警告'注意:未定義索引:第21行的/home/hue/test/controllers/SiteController.php中的文本 注意:未定義索引:/ home中的圖片第22行的/hue/test/controllers/SiteController.php –

回答

0

當are關鍵字時,您必須轉義字段名稱。所以你必須像這樣在場地文字周圍放回勾號。

$query = 'INSERT INTO tasks (username, email, `text`, picture, check_token) 
      VALUES (:username, :email, :text, :picture, :check_token)'; 
+1

爲什麼?它不是保留字,它是一個關鍵字https://dev.mysql.com/doc/refman/5.7/en/keywords.html,這不是問題,它是*「有此警告注意:未定義的索引:文本在第21行的/home/hue/test/controllers/SiteController.php中注意:未定義的索引:第22行中的/home/hue/test/controllers/SiteController.php中的圖片 - Alexandr Krivosheev 10分鐘前「* –

+0

test is像int,char varchar,text –

+1

這樣的數據類型當然可以,但是我希望在實際問題上有所不同,OP在評論中明確表示。同樣,手冊中沒有'(R)'旁邊的'TEXT',因此在這裏不需要轉義。 –