查询失败!列数与第1行的值计数不匹配

时间:2013-08-30 22:36:04

标签: php mysql sql

我想把它放到数据库中。 然而,我收到一个意外错误,说列数与第1行的值计数不匹配。查询有什么问题?

<?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!');
    }
?>

1 个答案:

答案 0 :(得分:1)

你这样做:

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

这需要您了解所有列及其序列。你需要全部使用它们。

但是,如果您知道可以省略哪些列,则可以使用此方法:

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

在此处,您可以指定要添加的列。如果所有其余列都定义了默认值或接受NULL,那么你就可以了。

相关问题