46

I'm getting this error:

Column count doesn't match value count at row 1

From the following code:

$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());

What does the error mean?

2
  • you have comment out // mysql_real_escape_string($image), your query waits 9 variables you pass it 8 Commented May 9, 2011 at 2:32
  • I am going to be uploading the image another way. The image key is in the table, but a value is not being inserted for it. Could this be a cause? Commented May 9, 2011 at 2:33

3 Answers 3

84

You have 9 fields listed, but only 8 values. Try adding the method.

Sign up to request clarification or add additional context in comments.

1 Comment

Then you didn't fix the problem. Because that is the problem.
13

The number of column parameters in your insert query is 9, but you've only provided 8 values.

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

The query should omit the "id" parameter, because it is auto-generated (or should be anyway):

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

1 Comment

Except all the fields are off by one now.
6

Your query has 8 or possibly even 9 variables, ie. Name, Description etc. But the values, these things ---> '', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", only total 7, the number of variables have to be the same as the values.

I had the same problem but I figured it out. Hopefully it will also work for you.

1 Comment

Please note that this question has already been answered. No need to revive a 1.5 year old post unless you can add something new. Thanks for your effort anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.