这是我来自产品表的三个产品的功能,我也在其中更新其数量(所有内容在这里都可以正常工作。]
问题是我想将数据插入订单表中的所有这三个产品中,不幸的是,它显示的是在回显中获取的数据,但没有插入数据库。
其第一个具有pro_id 1的产品kabrice:
function kabrice(){
include("dbconnection.php");
$getm_cart_item=$connect->prepare("select * from products where p_id='1'");
$getm_cart_item->setFetchMode(PDO:: FETCH_ASSOC);
$getm_cart_item->execute();
$cart_empty=$getm_cart_item->rowCount();
$net_total=0;
if($cart_empty==0){
echo "<center><h2>No Services found <a href='order.php'>Continue Shopping</a></h2></center>";
}else{
if(isset($_POST['up_qty'])){
$quantity=$_POST['qty'];
foreach($quantity as $key=>$value){
$update_qty=$connect->prepare("update products set punit='$value' where p_id='$key'");
if($update_qty->execute()){
echo "<script>window.open('order.php','_self');</script>";
}
}
}}
echo"<table cellpadding='5' cellspacing='5'>
<tr border='1'>
<th width=15%>Service Name</th>
<th width=20%>Quantity</th>
<th width=10%>Price</th>
<th width=20%>Sub Total</th>
</tr>";
while($row=$getm_cart_item->fetch()):
$get_pro=$connect->prepare("select * from products where p_id='1'");
$get_pro->setFetchMode(PDO:: FETCH_ASSOC);
$get_pro->execute();
$row_pro=$get_pro->fetch();
echo"<tr>
<td><input type='hidden'name='pname''>".$row_pro['pname']."</td>
<td><input type='number' name='qty[".$row['p_id']."]' value='".$row['punit']."'/><input type='submit'name='up_qty'value='Update Quantity'/></td>
<td><input type='hidden'name='pprice' >".$row_pro['pprice']." SZK - per month</td>
<td><input type='hidden'name='sub_total'>";
$qty=$row['punit'];
$pro_price=$row_pro['pprice'];
$sub_total=$pro_price*$qty;
echo $sub_total;
echo" SZK</td>
</tr>";
endwhile;
其第二个产品:
$getm_cart_item1=$connect->prepare("select * from products where p_id='2'");
$getm_cart_item1->setFetchMode(PDO:: FETCH_ASSOC);
$getm_cart_item1->execute();
$cart_empty1=$getm_cart_item1->rowCount();
$net_total1=0;
if($cart_empty1==0){
echo "<center><h2>No Services found <a href='order.php'>Continue Shopping</a></h2></center>";
}else{
if(isset($_POST['up_qty1'])){
$quantity1=$_POST['qty'];
foreach($quantity1 as $key1=>$value1){
$update_qty1=$connect->prepare("update products set punit='$value1' where p_id='$key1'");
if($update_qty1->execute()){
echo "<script>window.open('order.php','_self');</script>";
}
}
}}
while($row1=$getm_cart_item1->fetch()):
$get_pro1=$connect->prepare("select * from products where p_id='2'");
$get_pro1->setFetchMode(PDO:: FETCH_ASSOC);
$get_pro1->execute();
$row_pro1=$get_pro1->fetch();
echo"<tr>
<td><input type='hidden'name='pname1' > ".$row_pro1['pname']."</td>
<td><input type='number' name='qty[".$row1['p_id']."]' value='".$row1['punit']."'/><input type='submit'name='up_qty1'value='Update Quantity'/></td>
<td><input type='hidden'name='pprice1' >".$row_pro1['pprice']." CZK - per month</td>
<td><input type='hidden'name='sub_total1' >";
$qty1=$row1['punit'];
$pro_price1=$row_pro1['pprice'];
$sub_total1=$pro_price1*$qty1;
echo $sub_total1;
echo" CZK</td>
</tr>";
endwhile;
其第三个产品:
$getm_cart_item2=$connect->prepare("select * from products where p_id='3'");
$getm_cart_item2->setFetchMode(PDO:: FETCH_ASSOC);
$getm_cart_item2->execute();
$cart_empty2=$getm_cart_item2->rowCount();
$net_total2=0;
if($cart_empty2==0){
echo "<center><h2>No Services found <a href='order.php'>Continue Shopping</a></h2></center>";
}else{
if(isset($_POST['up_qty2'])){
$quantity2=$_POST['qty'];
foreach($quantity2 as $key2=>$value2){
$update_qty2=$con->prepare("update products set punit='$value2' where p_id='$key2'");
if($update_qty2->execute()){
echo "<script>window.open('order.php','_self');</script>";
}
}
}}
while($row2=$getm_cart_item2->fetch()):
$get_pro2=$connect->prepare("select * from products where p_id='3'");
$get_pro2->setFetchMode(PDO:: FETCH_ASSOC);
$get_pro2->execute();
$row_pro2=$get_pro2->fetch();
echo"<tr>
<td><input type='hidden'name='pname2' > ".$row_pro2['pname']."</td>
<td><input type='number' name='qty[".$row2['p_id']."]' value='".$row2['punit']."'/><input type='submit'name='up_qty2'value='Update Quantity'/></td>
<td><input type='hidden'name='pprice2' > ".$row_pro2['pprice']." SZK - per 6 months</td>
<td><input type='hidden'name='sub_total2' >";
$qty2=$row2['punit'];
$pro_price2=$row_pro2['pprice'];
$sub_total2=$pro_price2*$qty2;
$net_total2=$sub_total+$sub_total1+$sub_total2;
echo$sub_total2;
echo" SZK</td>
</tr>";
endwhile;
echo "<tr>
<td></td>
<br>
<td></td>
<td><b>Net Totoal</b></td>
<td><input type='hidden'name='net_total2' id='net_total2' ><b>$net_total2 SZK</b></td>
</tr>
";
''' ** insert query is here to insert fethed data into orders table**
'''
if(isset($_POST) && array_key_exists('submit',$_POST))
{
$pname =$row_pro['pname'];
$pprice =$row_pro['pprice'];
$pname1 =$row_pro1['pname1'];
$pprice1 =$row_pro1['pprice1'];
$pname1 =$row_pro2['pname2'];
$pprice1 =$row_pro2['pprice2'];
//$ticfile=$_FILES["tfile'"]["name"];
$pdate1=date('Y-m-d');
//move_uploaded_file($_FILES["tfile"]["tmp_name"],"ticketfiles/".$_FILES["tfile"]["name"]);
$a=mysqli_query($con,"insert into orders(pname,price,pname1,pprice1,pname2,pprice2, posting_date) values('$pname','$price','$pname1','$pprice1','$pname2','$pprice2','$pdate1')");
echo "insert into orders(pname,price,pname1,pprice1,pname2,pprice2, posting_date) values('$pname','$price','$pname1','$pprice1','$pname2','$pprice2','$pdate1')";
if(!$a)
{
echo "<script>alert('Order Generated');</script>";
}
}
}
任何人都可以帮助我解决此问题吗?
如此处所示:
$getm_cart_item2 = $connect->prepare("select * from products where p_id='3'");
您正在使用$connect
作为连接,但在这里:
$a = mysqli_query($con,"insert into orders(pname,price,pname1,pprice1,pname2,pprice2, posting_date) values('$pname','$price','$pname1','$pprice1','$pname2','$pprice2','$pdate1')");
您使用了$con
。现在,您与数据库建立了两个连接,或者这是您的错误。