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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作