如何获取所选下拉框的值并插入数据库?

时间:2015-03-23 07:34:01

标签: php mysql

我刚刚创建了一个数量下拉框,唯一的一个是当点击添加购物车按钮时,下拉框的数量或选定值没有插入到表名称购物车中,列数量请帮助..下面是代码来自functions.php和product.php

<div id="products_box">
        <?php

 $get_pro = "select * from products ";
 $run_pro = mysqli_query($con, $get_pro);
 while($row_pro=mysqli_fetch_array($run_pro)){
    $pro_id = $row_pro['product_id'];
    $pro_cat = $row_pro['product_cat'];
    $pro_brand = $row_pro['product_brand'];
    $pro_title = $row_pro['product_title'];
    $pro_price = $row_pro['product_price'];
    $pro_image = $row_pro['product_image'];
    $pro_qty = $row_pro['product_qty'];
    echo "
        <div id='single_product'>
        <h4>$pro_title</h4>
        <img src='admin_area/product_images/$pro_image' width='180' height='80' />
        <p><b> Php $pro_price.00</b></p>
        <a href='details.php?pro_id=$pro_id' style='float:left;'>Details</a>
        <select name='quantity'>";
        for($i=1;$i<=$pro_qty;$i++) {
         echo  "<option value=$i>$i</option>";
          }
        echo  "</select>

        <a href='index.php'?pro_id=$pro_id'><button style='float:right'>Add to Cart</button></a>
        </div>
        ";

}
?>
        </div>


function cart(){
if(isset($_GET['add_cart'])){
    global $con;
    $ip = getIp();
    $pro_id = $_GET['add_cart'];
    $pro_qty = $_POST['quantity'];
    $check_pro = "select * from cart where ip_add='$ip' AND p_id='$pro_id'";
    $run_check = mysqli_query($con, $check_pro);
    if(mysqli_num_rows($run_check)>0){
        echo ""; //refresh or do nothing
    }
    else {
    $insert_pro = "insert into cart(p_id,ip_add,qty) values ('$pro_id','$ip','$pro_qty')";
    $run_pro = mysqli_query($con, $insert_pro);
    echo "<script>window.open('index.php','_self')</script>";
    }

} }

1 个答案:

答案 0 :(得分:1)

请你试试这个

<div id="products_box">
        <?php

 $get_pro = "select * from products ";
 $run_pro = mysqli_query($con, $get_pro);
 while($row_pro=mysqli_fetch_array($run_pro)){
    $pro_id = $row_pro['product_id'];
    $pro_cat = $row_pro['product_cat'];
    $pro_brand = $row_pro['product_brand'];
    $pro_title = $row_pro['product_title'];
    $pro_price = $row_pro['product_price'];
    $pro_image = $row_pro['product_image'];
    $pro_qty = $row_pro['product_qty'];
    echo "
        <form method='post' action='index.php'>
        <input type='hidden' value= '".$pro_id."' name ='pro_id' />
        <div id='single_product'>
        <h4>$pro_title</h4>
        <img src='admin_area/product_images/$pro_image' width='180' height='80' />
        <p><b> Php $pro_price.00</b></p>
        <a href='details.php?pro_id=$pro_id' style='float:left;'>Details</a>
        <select name='quantity'>";
        for($i=1;$i<=$pro_qty;$i++) {
         echo  "<option value=$i>$i</option>";
          }
        echo  "</select>

        <input type='submit' name='add' value='Add to Cart'>
        </div>
        </form>
        ";

}
?>
        </div>


function cart(){
if(isset($_GET['add_cart'])){
    global $con;
    $ip = getIp();
    $pro_id = $_POST['pro_id'];
    $pro_qty = $_POST['quantity'];
    $check_pro = "select * from cart where ip_add='$ip' AND p_id='$pro_id'";
    $run_check = mysqli_query($con, $check_pro);
    if(mysqli_num_rows($run_check)>0){
        echo ""; //refresh or do nothing
    }
    else {
    $insert_pro = "insert into cart(p_id,ip_add,qty) values ('$pro_id','$ip','$pro_qty')";
    $run_pro = mysqli_query($con, $insert_pro);
    echo "<script>window.open('index.php','_self')</script>";
    }
    }
    }