dongming6201 2016-03-03 07:43
浏览 336
已采纳

错误在将数据插入到数据库时,列计数与第1行的值计数不匹配

I have this e-commerce site I am working on. Now I keep getting that error every time I try to insert data to the db. I am trying to insert details on table buyer and message but I don't know where the error is.

I don't know where the error is please help me out:

My code:

$buyer_name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$mobile_no = $_POST['mobile_no'];
$id_no = $_POST['id_no'];
$total = $_POST['total'];

echo '<a href="index.php">Home</a><br>';

echo 'Thank you for shopping at our Thread online store. And the following is the data you enter.';
echo '<p>The total cost for the purchase of products is:Tsh '.$total.
'/=, and the cost can be send through the M-Pesa mobile number +255 757 393 937, Airtel Money: # and Tigo-Pesa: #.</p>';
echo '<p>And the goods will be sent to the address below:</p>';
echo '<p>Name : '.$buyer_name.
'<br>';
echo '<p>Address and Location : '.$address.
'</p>';
echo '<p>Total Price : '.$total.
'</p>';
echo '<p>With Details : </p>';

$p = mysql_query("SELECT * FROM buyer");
$cek = mysql_fetch_array($p);
$c1 = $cek['name'];
$c2 = $cek['address'];
$c3 = $cek['email'];
$c4 = $cek['mobile_no'];
$c5 = $cek['id_no'];

if ($c1 == $buyer_name && $c2 == $address && $c3 == $email && $c4 == $mobile_no && $c5 == $id_no) {
    $i = 1;
    foreach($_SESSION as $name => $value) {
        if ($value > 0) {
            if (substr($name, 0, 5) == 'cart_') {
                $id = substr($name, 5, (strlen($name) - 5));
                $get = mysql_query("SELECT * FROM products WHERE product_id='$id'");
                while ($get_row = mysql_fetch_assoc($get)) {
                    $sub = $get_row['product_price'] * $value;

                    echo ' < table class = "table table-striped" >
                        < thead >
                        < tr >
                        < th > # < /th> < th > Product ID < /th> < th > Product Name < /th> < th > Quantity < /th> < /tr> < /thead> < tbody >
                        < tr >
                        < td > '.$i.' < /td> < td > '.$get_row['
                    product_id '].' < /td> < td > '.$get_row['
                    product_title '].' < /td> < td > '.$value.' < /td> < /tr> < /tbody < /table>';
                        //<p>'.$i.' '.$get_row['product_id'].' '.$get_row['product_title'].' '.$value.' SubTotal : Tsh '.$sub.' /=</p>                                      

                    $getBuyer = mysql_query("SELECT buyer.id, buyer.name, buyer.address, products.product_id, products.product_title FROM buyer, products WHERE name='$buyer_name' AND address='$address'") or die(mysql_error());

                    $data = mysql_fetch_array($getBuyer);

                    $pemb = $data['id'];
                    $na = $data['name'];
                    $al = $data['address'];
                    $ib = $get_row['product_id'];
                    $nb = $get_row['product_title'];

                    //echo $total;
                    $i++;
                }
            }
            //@$total += $sub;
            mysql_query("INSERT INTO message VALUES('', '$pemb', '$na', '$al', '$ib', '$nb', '$value', '$sub', now()) ") or die(mysql_error());
        }
    }
} else {
    //Insert Data of the buyer to the db
    $query = mysql_query("INSERT INTO buyer VALUES( '$buyer_name', '$email', '$address', '$mobile_no', '$id_no')") or die(mysql_error());

    $i = 1;
    foreach($_SESSION as $name => $value) {
        if ($value > 0) {
            if (substr($name, 0, 5) == 'cart_') {
                $id = substr($name, 5, (strlen($name) - 5));
                $get = mysql_query("SELECT * FROM products WHERE product_id='$id'");
                while ($get_row = mysql_fetch_assoc($get)) {
                    $sub = $get_row['product_price'] * $value;

                    echo ' < table class = "table table-striped" >
                        < thead >
                        < tr >
                        < th > # < /th> < th > Product ID < /th> < th > Product Name < /th> < th > Quantity < /th> < /tr> < /thead> < tbody >
                        < tr >
                        < td > '.$i.' < /td> < td > '.$get_row['
                    product_id '].' < /td> < td > '.$get_row['
                    product_title '].' < /td> < td > '.$value.' < /td> < /tr> < /tbody < /table>';  

                    $getBuyer = mysql_query(" SELECT buyer.id, buyer.name, buyer.address, products.product_id, products.product_title FROM buyer, products WHERE name='$buyer_name' AND address='$address'") or die(mysql_error());

                    $data = mysql_fetch_array($getBuyer);

                    $pemb = $data['id'];
                    $na = $data['name'];
                    $al = $data['address'];
                    $ib = $get_row['product_id'];
                    $nb = $get_row['product_title'];

                    //echo $total;
                    $i++;
                }
            }
            //@$total += $sub;
            mysql_query("INSERT INTO message VALUES('', '$pemb', '$na', '$al', '$ib', '$nb', '$value', '$sub', now()) ") or die(mysql_error());
        }
    }
}



/*if ($query) 
                              {
                                  header('location:index.php');
                              }*/

session_destroy();
  • 写回答

1条回答 默认 最新

  • dqdl6469 2016-03-03 07:55
    关注

    You must define field name for insert

    Instead

    $query = mysql_query("INSERT INTO buyer VALUES( '$buyer_name', '$email', '$address', '$mobile_no', '$id_no')") or die(mysql_error());  
    

    Use like this for insert to buyer

    $query = mysql_query("INSERT INTO buyer(name,email,address,mobile,id) VALUES( '$buyer_name', '$email', '$address', '$mobile_no', '$id_no')") or die(mysql_error());  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog