Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

TABLE COLUMNS theid sirename yob color breed owner approved

FORM TO GATHER INFO TO INSERT INTO DB

<form method='post' action='nominateasire2.php'>
The ID: <input type='text' name='theid'><br><br>
Sire Name: <input type='text' name='sirename'><br><br>
YOB: <input type='text' name='yob'><br><br>
Color: <input type='text' name='color'><br><br>
Breed: <input type='text' name='breed'><br><br>
Owner: <input type='text' name='owner'><br><br>
<input type='submit' value='Nominate'>
</form>

PHP TO INSERT INFO FROM FORM INTO DB

$sql="INSERT INTO nominatedsires (theid,sirename,yob,color,breed,owner,approved)
VALUES ('$_POST[theid]','$_POST[sirename]','$_POST[yob]',
'$_POST[color]','$_POST[breed]','$_POST[owner]','$_POST[owner]','no')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

When using the form I get the error Column count doesn't match value count at row 1. When I go through PHPmyAdmin and input the data it all works fine so there isn't an error on the backend (value length/type/etc.) I can't figure out what the issue with my code is. Any help greatly appreciated!

share|improve this question
    
When using mysqli you should be using parameterized queries and bind_param to add user data to your query. Avoid using string interpolation to accomplish this. Adding $_POST data directly to a query is extremely dangerous. –  tadman Feb 5 at 20:11
add comment

3 Answers

up vote 0 down vote accepted

you have '$_POST[owner]', twice. Remove one of them and you should be ok.

share|improve this answer
    
Wow, I can't believe I missed that. That is definitely a face palm moment. Works great now. –  ld04 Oct 23 '13 at 19:51
    
no, problem, I make those mistakes all the time too. Cheers –  Jim Oct 24 '13 at 11:59
add comment

You have an extra column in your list of values. $_POST[owner] is there twice.

share|improve this answer
add comment

Your previous code is:

$sql="INSERT INTO nominatedsires (theid,sirename,yob,color,breed,owner,approved)
VALUES ('$_POST[theid]','$_POST[sirename]','$_POST[yob]',
'$_POST[color]','$_POST[breed]','$_POST[owner]','$_POST[owner]','no')";


This is the correct code:

$sql="INSERT INTO nominatedsires (theid,sirename,yob,color,breed,owner,approved)
VALUES ('$_POST[theid]','$_POST[sirename]','$_POST[yob]',
'$_POST[color]','$_POST[breed]','$_POST[owner]','no')";
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.