简体   繁体   中英

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;

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')";

If your user_master table has any primary key like id or user_id , then you can do two things:

  • You can add auto_increment attribute on "id" field
  • Add primary value for "id".

I had the same problem... the way I solved it was by making sure to not put a comma in my integer values .

so if you are inputing a salary into an html form, do not put commas in the salary figure. (I am following the examples given on tutorials point. i thought the salary box should contain a comma, but it caused this error.)

Make sure that your sql query doesn't have spaces near the staring and ending parenthesis. Below will cause the error you mentioned:

$sql = "INSERT INTO table_name ( email, username ) VALUES ($userEmail,$username)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM