[英]PHP Select Option Does Not Insert Value
I'm making a CMS for my Online Store with PHP and MySQL. 我正在使用PHP和MySQL为我的在线商店制作CMS。 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: 现在,在
insertproducts.php
我还添加了以下行以插入新项目:
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
: 但是当我检查
PHPMYADMIN
,对于插入的产品,我看到0作为product_cat
的值:
However, the value of this field must something like this (according to the categories
table): 但是,此字段的值必须类似于以下内容(根据
categories
表):
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.
试试这个,不发表您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.