2011-05-09 67 views
44

我得到這個錯誤:PHP,MySQL錯誤:列計數不在行匹配值計數1

Column count doesn't match value count at row 1

從下面的代碼:

$name = $_GET['name']; 
$description = $_GET['description']; 
$shortDescription = $_GET['shortDescription']; 
$ingredients = $_GET['ingredients']; 
$method = $_GET['method']; 
//$image = $_GET['image']; 
$username = $_GET['username']; 
$length = $_GET['length']; 
$dateAdded = uk_date(); 
$conn = mysql_connect('localhost', 'dbname', 'pass'); 
mysql_select_db('dbname'); 
$query = sprintf("INSERT INTO dbname (id, Name, Description, shortDescription, Ingredients, Method, Length, dateAdded, Username) VALUES ('', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", 
    mysql_real_escape_string($name), 
    mysql_real_escape_string($description), 
    mysql_real_escape_string($shortDescription), 
    mysql_real_escape_string($ingredients), 
    //mysql_real_escape_string($image), 
    mysql_real_escape_string($length), 
    mysql_real_escape_string($dateAdded), 
    mysql_real_escape_string($username)); 

    $result = mysql_query($query) or die(mysql_error()); 

什麼的錯誤的意思?

+0

你有評論''mysql_real_escape_string($ image), '你的查詢等待你傳遞它的9個變量8 – afarazit 2011-05-09 02:32:55

+0

我將以另一種方式上傳圖像。圖像鍵位於表格中,但未插入值。這可能是一個原因嗎? – 2011-05-09 02:33:40

回答

81

您有9個字段列出,但只有8個值。嘗試添加該方法。

+14

然後你沒有解決這個問題。因爲那*是問題。 – 2011-05-09 02:33:47

13

插入查詢中列參數的數量是9,但您只提供了8個值。

INSERT INTO dbname (id, Name, Description, shortDescription, Ingredients, Method, Length, dateAdded, Username) VALUES ('', '%s', '%s', '%s', '%s', '%s', '%s', '%s') 

查詢應省略 「ID」 參數,因爲它是自動生成的(或者說應該是這樣):

INSERT INTO dbname (Name, Description, shortDescription, Ingredients, Method, Length, dateAdded, Username) VALUES ('', '%s', '%s', '%s', '%s', '%s', '%s', '%s') 

祝你好運!

+0

除了現在所有的字段都被關閉。 – 2011-05-09 02:39:00

6

你的查詢有8個甚至9個變量,即。名稱,描述等但值,這些東西--->'', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",只有總數爲7,變量的數量必須與數值相同。

我有同樣的問題,但我想通了。希望它也能爲你工作。

+3

請注意,這個問題已經得到解答。除非你可以添加新的東西,否則不需要恢復1.5歲的帖子。無論如何感謝您的努力。 – fancyPants 2012-10-02 07:32:03

相關問題