简体   繁体   中英

PHP Select Option Does Not Insert Value

I'm making a CMS for my Online Store with PHP and MySQL. Basically I have made a page where Admins of a site can add new product items to the store via that. Every product has a Product Category field. Now in order to print all the available categories of the shop into a select option field I did this:

<div class='form-group'>
<label><span style='color:red'>*</span> Product Category:</label>
<select class='form-control select2' name='product_cat' style='width: 100%;'>
".get_cats()."
</select>
</div>

Now this function is referring to this:

    <?php 
function get_cats(){
    $get_cats = "select * from categories";
    $run_cats = mysqli_query($GLOBALS['con'],$get_cats);
    $return = '';
    while($row_cats=mysqli_fetch_array($run_cats)){
        $cat_id = $row_cats['cat_id'];
        $cat_title = $row_cats['cat_title'];
        $return .= "
            <option value='$cat_id'>$cat_title</option>
        ";
    }
    return $return;
}
?>

Now in the insertproducts.php I added this line as well in order to insert new items:

if(isset($_POST['insert_product'])){
    $product_cat = $_POST['product_cat'];

    $insert_product = "INSERT INTO `products` (`product_cat`) VALUES $product_cat";

    $run_product = mysqli_query($con,$insert_product);
}

But when I check in PHPMYADMIN , for the inserted product, I see 0 as the value of product_cat :

print screen

However, the value of this field must something like this (according to the categories table):

print screen 2

So what's the problem here? Can you please help me with that?

尝试在while循环中添加return $ return

If you want to add any new product via admin panel you need input type text. Using text field only we can add a product dynamically. After product added you can view using dropdown menu. Try this and isn't working post your questions.

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