Jess Tan
Jess Tan

Reputation: 23

Query Failed!Column count doesn't match value count at row 1

I am trying to put this into the database. And i am getting an unexpected error, however, saying Column count doesn't match value count at row 1. What is wrong with the query?

<?php 
    if($_REQUEST['command']=='update'){
    $charge   = $_REQUEST['ocharge'];
    $fname    = $_REQUEST['ofname'];
    $lname    = $_REQUEST['olname'];
    $mobile   = $_REQUEST['omobile'];
    $add1     = $_REQUEST['oadd1'];
    $add2     = $_REQUEST['oadd2'];
    $postcode = $_REQUEST['opostcode'];
    $state    = $_REQUEST['ostate'];
    $country  = $_REQUEST['ocountry'];
    $weight   = $_REQUEST['oweight'];
    $credit   = $_REQUEST['ocredit'];
    $pin      = $_REQUEST['opin'];
    $city     = $_REQUEST['ocity'];

        $date=date('Y-m-d');
        $time=time('H:i:s');
        $result=mysql_query("insert into order values ('$date','$time','$charge','$fname','$lname','$mobile','$add1','$add2','$postcode','$state','$country','$weight','$credit','$pin','$city')");
        $orderid=mysql_insert_id();    

        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=$_SESSION['cart'][$i]['qty'];
            $price=get_price($pid);
            $result1=mysql_query("insert into order_detail values ('$orderid','$pid','$q','$price')");
            if($result1 === FALSE)
            {
                die("Query Failed!".mysql_error().$result1);
            }

        }
        die('Thank You! your order has been placed!');
    }
?>

Upvotes: 0

Views: 4711

Answers (1)

Grzegorz
Grzegorz

Reputation: 3335

You do something like that:

INSERT INTO table_name
VALUES (value1, value2, value3,...)

Which requires for your knowing all columns, and their sequence. You need to use them all.

However, if you know which columns you may omit, you can use this method:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

Here, you specify which columns you want to add. If all the rest of columns have defined default values or accept NULL, you will be okay.

Upvotes: 2

Related Questions