简体   繁体   中英

How to get values of selected dropdown box and insert to database?

I just created a quantity drop down box, the only thing is when add cart button is clicked, the quantity or the selected value of dropdown box is not inserting to table name cart with column qty please help.. below is code from functions.php and 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>";
    }

} }

Can you please give a try to this

<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>";
    }
    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM