2

我想将购物车详细信息插入 mysql 数据库,但它在 mysql 数据库中显示空白字段

这是我的页面代码

<?php 
include('config.php');
$id=$_REQUEST['id'];
include("cart/db.php");
include("cart/functions.php");
if($_REQUEST['command']=='delete' && $_REQUEST['id']>0){
remove_product($_REQUEST['id']);
}
else if($_REQUEST['command']=='clear'){
unset($_SESSION['cart']);
}
else if($_REQUEST['command']=='update'){ 
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
$id=$_SESSION['cart'][$i]['id'];
$q=intval($_REQUEST['product'.$id]);
if($q>0 && $q<=999){
$_SESSION['cart'][$i]['qty']=$q;
}
else{
$msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
}
}
}
if(isset($_REQUEST['order']))
{
$product=$_POST['product']; 
$queryproduct=mysql_query("INSERT INTO shoppingcart VALUES     ('','','','$product','','','','')") or die("Order  Query Problem");  

}
?>

这里是我的购物车表,我想插入到 Mysql 数据库表中

<form name="form1" method="post">  
<input type="hidden" name="id" />
<input type="hidden" name="command" />  
<div class="main_content">
<div align="center">
<table style="background-color:#F0F0F0; border:solid 1px; width:800px;">
<?php
        if(is_array($_SESSION['cart'])){
            echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td align="center"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Product Name</font></td><td align="center"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Product Image</font></td><td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Price</font></td><td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Qty</font></td><td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Amount</font></td><td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000;">Options</font></td></tr>';
            $max=count($_SESSION['cart']);
            for($i=0;$i<$max;$i++){
                $id=$_SESSION['cart'][$i]['id'];
                $q=$_SESSION['cart'][$i]['qty'];
                $product=get_product_name($id);
                $image=get_product_image($id);
                if($q==0) continue;
        ?>
                <tr bgcolor="#FFFFFF"><td align="center"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><?php echo $product?></font></td><td align="center"><img src="admin/uploads/small0_<?php echo $image?>" width="150" height="150"></td>
                <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;">Rs.<?php echo get_price($id)?></font></td>
                <td><input type="text" name="product<?php echo $id?>" value="<?php echo $q?>" maxlength="3" size="2" /></td>                    
                <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;">Rs.<?php echo get_price($id)*$q?></font></td>
                <td><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;"><a href="javascript:del(<?php echo $id?>)"><input type="button" class="button5" value="Remove" /></a></font></td></tr>
        <?php                   
            }
        ?>

            <tr><td colspan="6" align="right"><b><font style="font-family:Arial,Helvetica,sans-serif;  font-size:15px; color:#000;">Order Total: Rs.<?php echo get_order_total()?></font></b></td></tr>        
            <tr><td colspan="6" align="right"><font style="font-family:Arial,Helvetica,sans-serif;  font-size:14px; color:#000; !important">Cash On Delivery & Free Shipping</font></td></tr>  
            <tr><td colspan="6" align="right"><?php $query1=mysql_query("SELECT * FROM category ORDER BY id ") or die ('Product Query Problem');
            $row4=mysql_fetch_array($query1);$ids=$row4['id'];$cat_name=$row4['categories'];?><input type="button" class="button1" value="Continue Shopping"  onclick="window.location='product.php'" /><input type="button" class="button2"  value="Clear Cart" onclick="clear_cart()"><input type="button" class="button3" value="Update Cart" onclick="update_cart()"><input type="button" class="button4" name="order" id="order" value="Place Order" onclick="window.location='login.php'"></td></tr>
        <?php
        }
        else{
            echo "<tr bgColor='#FFFFFF'><td><font style='font-family:Arial,Helvetica,sans-serif; font-size:15px; color:#000;'>There are no items in your shopping cart!</font></td>";
        }
    ?>
    </table>

我从此页面从数据库中获取产品 ID 和全部

<?php
 $id=$_REQUEST['id'];
function get_product_name($id){
    $result=mysql_query("select * from products where id='$id'") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
    $row=mysql_fetch_array($result);
    return $row['product'];
}

function get_product_image($id){
    $result=mysql_query("select * from products where id='$id'") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
    $row=mysql_fetch_array($result);
    return $row['image'];
}


function get_price($id){
    $result=mysql_query("select * from products where id='$id'") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
    $row=mysql_fetch_array($result);
    return $row['price'];
}
function remove_product($id){
    $id=intval($id);
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        if($id==$_SESSION['cart'][$i]['id']){
            unset($_SESSION['cart'][$i]);
            break;
        }
    }
    $_SESSION['cart']=array_values($_SESSION['cart']);
}
function get_order_total(){
    $max=count($_SESSION['cart']);
    $sum=0;
    for($i=0;$i<$max;$i++){
        $id=$_SESSION['cart'][$i]['id'];
        $q=$_SESSION['cart'][$i]['qty'];
        $price=get_price($id);
        $sum+=$price*$q;
    }
    return $sum;
}
function addtocart($id,$q){
    if($id<1 or $q<1) return;

    if(is_array($_SESSION['cart'])){
        if(product_exists($id)) return;
        $max=count($_SESSION['cart']);
        $_SESSION['cart'][$max]['id']=$id;
        $_SESSION['cart'][$max]['qty']=$q;
    }
    else{
        $_SESSION['cart']=array();
        $_SESSION['cart'][0]['id']=$id;
        $_SESSION['cart'][0]['qty']=$q;
    }
}
function product_exists($id){
    $id=intval($id);
    $max=count($_SESSION['cart']);
    $flag=0;
    for($i=0;$i<$max;$i++){
        if($id==$_SESSION['cart'][$i]['id']){
            $flag=1;
            break;
        }
    }
    return $flag;
}

?>

4

1 回答 1

0

在这里,您没有命名的输入文本product,因此您 $_POST['product']将始终为空。所以它在数据库中被添加为空

更改$product$q将插入值:

$queryproduct=mysql_query("INSERT INTO shoppingcart VALUES ('','','','$product','','','','')") or die("Order Query Problem");

于 2013-03-01T11:14:48.610 回答