2014-10-10 26 views
0

我嘗試用準備好的發言首次運行到以下問題與下面的代碼PHP的MySQL綁定PARAM參數

錯誤:

警告:mysqli_stmt_bind_param()預計參數1要 mysqli_stmt,布爾給

代碼:

$stmt = mysqli_prepare($db, "INSERT INTO fragrances(name, description, essentialoils, topnotes, middlenotes, basenotes, reference, year, type, price, fragrancehouse, triangle, extractname, extractreference, extractprice, extractfragrancehouse, disccolour, collarcolour, actuatorcolour) 
    VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 
      mysqli_stmt_bind_param($stmt, 'sssssssssssssssssss', $name, $description, $essentialoils, $topnotes, $middlenotes, $basenotes, $reference, $year, $type, $price, $fragrancehouse, $triangle, $extractname, $extractreference, $extractprice, $extractfragrancehouse, $disccolour, $collarcolour, $actuatorcolour); 

     mysqli_stmt_execute($stmt); 

我在這裏看過很多不同的問題,他們的解決方案似乎都不適用於我的問題,有誰知道這個問題是什麼?

筆記,我所有的瓦爾都是字符串

感謝

+0

準備失敗。你需要得到'mysqli_error'的輸出。您的查詢中也沒有佔位符。如果mysqli支持使用'?'或命名參數(我不確定) – Cfreak 2014-10-10 20:06:42

回答

4

$stmt成爲一個布爾值,只有當mysqli_prepare返回false。

當發生這種情況就意味着它沒有準備查詢,因此,你需要檢查錯誤:

$stmt = mysqli_stmt_init($db); 
if (mysqli_stmt_prepare($stmt, 'INSERT INTO fragrances VALUES...')) { 
    //it's all good bind and execute here 
}else{ 
    //we have a problem 
    printf("Errormessage: %s\n", mysqli_error($db)); 
} 
+0

原來我混了我的美國人,並錯誤地命名了collarcolor!這導致我的錯誤,所以得到了滴答聲,謝謝 – Xand94 2014-10-10 20:35:44

2

的錯誤消息意味着您mysqli_prepare返回一個布爾值(和你的情況下,它返回false)。

您需要用字符?來替換您的所有字段名稱,以制定您的預備聲明。這是它的工作原理。

See example in the official documentation

編輯See also mysqli_error,其中將詳細介紹你的錯誤。事實上,你應該總是使用它之前檢查變量:

$stmt = mysqli_prepare($db, "...."); 
if(!$stmt) 
    echo mysqli_error($db); // display error only for debug. Avoid this in production 
+0

不幸的是,這並沒有改變我的錯誤信息 – Xand94 2014-10-10 20:29:49

+0

所以你有其他錯誤。可能與'$ db'有關。我編輯了我的答案,以幫助您調試它。 – Asenar 2014-10-10 20:34:56

2

INSERT聲明是無效的:VALUES條款必須與括號?(並在括號中的字段名稱後)。還不錯的做法是分配後檢查$stmt

$stmt = mysqli_prepare($db, 
    "INSERT INTO fragrances (name, description, essentialoils, topnotes, middlenotes, basenotes, reference, year, type, price, fragrancehouse, triangle, extractname, extractreference, extractprice, extractfragrancehouse, disccolour, collarcolour, actuatorcolour) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 
if ($stmt) { 
    mysqli_stmt_bind_param($stmt, 'sssssssssssssssssss', $name, $description, $essentialoils, $topnotes, $middlenotes, $basenotes, $reference, $year, $type, $price, $fragrancehouse, $triangle, $extractname, $extractreference, $extractprice, $extractfragrancehouse, $disccolour, $collarcolour, $actuatorcolour); 

    mysqli_stmt_execute($stmt); 
    // ... 
} else 
    printf("Error: %s\n", mysqli_error($db)); 
+0

不幸的是,仍然創建相同的錯誤信息(刪除if語句) – Xand94 2014-10-10 20:29:30

2

這意味着你的SQL是無效的,因爲prepare是返回false;

你的SQL應該是;

$stmt = mysqli_prepare($db, "INSERT INTO fragrances VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 

每個?是顯示每個參數需要分別綁定的地方。