gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:02:22 25 4
gpt4 key购买 nike

我刚刚创建了一个数量下拉框,唯一的问题是当点击添加购物车按钮时,下拉框的数量或所选值没有插入到带有数量列的表名购物车中,请帮助..下面是代码来自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>";
}

}

最佳答案

你能试试这个吗

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

关于php - 如何获取所选下拉框的值并插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29205150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com