powek posallas
powek posallas

Reputation: 49

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

} }

Upvotes: 1

Views: 539

Answers (1)

Anto S
Anto S

Reputation: 2449

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

Upvotes: 1

Related Questions