我有一个表格,用户可以在其中输入产品 ID 及其数量。我想要实现的是当用户提交表单时,我想在控制器中检查数据库以查看所提供产品的数量是否有库存,如果没有,我想在表单中显示验证错误消息。如果一切正常,我想继续下一步。
如果我想使用 Codeigniter 的默认验证库来实现这一点,你能告诉我我的控制器应该是什么样子吗?
这是我的数据库表名:产品
product_id category_id product_name product_price product_stock
1 1 Mango Juice 25 100
2 2 Pepsi 10 0
这是我的查看文件——我的表格(查看此链接查看表格)
<form name="form" action="base_url/my_controller/function" method="post">
<label>One</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<label>Two</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<!-- there may be more inputs like above (users can create new inputs
as many as they want) // I have a jquery function to create new rows-->
<input type="submit" value="submit">
</form>
编辑部分新
function test(){
$productid = ($_POST['productid']);
$quantity = ($_POST['quantity']);
for($i = 0; $i < count($productid); $i++){
$result=$this->enquiry($productid[$i],$quantity[$i]);
}
/* Now I am stuck here. I don't understand how to find out if the
products are available or not. If not I want to show the
error message in the form :( */
}//function ends
function enquiry($productid,$quantity){
$query = $this->db->query("SELECT product_stock FROM products
WHERE product_id=$productid");
if ($query->num_rows() > 0){
foreach ($query->result() as $row){
$product_stock=$row->product_stock;
}
}
if($product_stock>$quantity) { return FALSE; }
else { return TRUE; }
}//function Ends