PHP MVC CodeIgniter Laravel Core PHP MORE

How to insert multiple chcekbox value in database using PHP MVC


In this example we using Models, Views, Controller Structure for insert multiple check value.

Sql table

Here is the Controller file hello.php which inside controllers folder

controllers/hello.php

<?php
class Hello extends Controller {
function __construct() {
parent::__construct();
}
function multi_check(){
if(isset($_POST['save']))
{
$category_id=1;
$checkbox = $_POST['check'];
for($i=0;$i $check_id = $checkbox[$i];
$this->model->multiple_insert($category_id,$check_id);
}
echo "Data inserted successfully !;
}
$this->view->render('hello/multi_check');
}
}
?>

models/hello_model.php

hello_model.php

<?php
cclass Hello_Model extends Model
{
public function __construct()
{
parent::__construct();
}
public function multiple_insert($category_id,$check_id)
{
return $this->db->insert("insert into checkbox (category_id,subcategory_id) values ('$category_id','$check_id')");
}
}
?>

Here is the view file multi_check.php which inside views folder contains the form.

views/hello/multi_check.php

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="checkbox" id="checkItem" name="check[]" value="1">
<input type="checkbox" id="checkItem" name="check[]" value="2">
<input type="checkbox" id="checkItem" name="check[]" value="3">
<input type="checkbox" id="checkItem" name="check[]" value="4">
<button type="submit" class="btn btn-primary" style="width:200px" name="save">Submit</button>
</form>
</body>
</html>
Path: localhost/project_folder_name/view_folder_name/view_filename
Example: localhost/mvc/hello/multi_check