Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column count doesn't match value count at row 1 php mysql

I'm getting the following error when I click the "AD RECORD" button:

INSERT failed: INSERT INTO user_master VALUES('cha' , 'rstein' , 'bar' , 'foo') Column count doesn't match value count at row 1

from the following code:

    if (isset($_POST['delete']) && isset($_POST['first']))
{
    $first = get_post('first');
    $query = "DELETE FROM user_master WHERE first='$first'";
    if(!mysql_query($query, $db_server))
        echo "DELETE failed: $query<br />" . 
        mysql_error() . "<br /><br />";
}

if (isset($_POST['first']) && isset($_POST['last']) && isset($_POST['user_name']) && isset($_POST['email']))
{
    $first      = get_post('first');
    $last       = get_post('last');
    $email      = get_post('email');
    $user_name  = get_post('user_name');

    $query = "INSERT INTO user_master VALUES" . "('$first' , '$last' , '$user_name' , '$email')";

    if(!mysql_query($query, $db_server)) echo "INSERT failed: $query <br />" . mysql_error() . "<br /><br />";
}

echo <<<END
<form action = "willingLog.html" method="post"><pre>
    First       <input type="text" name="first" />
    Last        <input type="text" name="last" />
    Email       <input type="text" name="email" />
    Username    <input type="text" name="user_name" />
            <input type="submit" value="AD RECORD" />
</pre></form>
    END;
like image 319
DBWeinstein Avatar asked Dec 15 '22 22:12

DBWeinstein


1 Answers

Your database table has more columns then you are inserting into so you're getting error. (You're probably not representing your userID field which probably is your primary key). You need to specify which fields the data is for in your query:

 $query = "INSERT INTO user_master (first_name, last_name, user_name, email) VALUES" . "('$first' , '$last' , '$user_name' , '$email')";
like image 93
John Conde Avatar answered Feb 11 '23 05:02

John Conde