CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

How to insert Multiple Checkbox value in CodeIgniter framework MySQL


In this example we are going to show you how to how to insert Multiple Checkbox value in CodeIgniter framework MySQL PHP.

Here we using 3 files for insert Multiple Checkbox value in CodeIgniter framework MySQL PHP:

  • Crud.php Path: codeIgniter\application\controllers\Crud.php
  • Crud_model.php Path: codeIgniter\application\models\Crud_model.php
  • multicheck_insert.php Path: codeIgniter\application\views\multicheck_insert.php

Crud.php (Controller)

<?php 
	class Crud extends CI_Controller 
	{
		public function __construct()
		{
			parent::__construct();/* call CodeIgniter's default Constructor */
			$this->load->database();/* load database libray manually */
			$this->load->model('Crud_model');/* load Model */
		}
		public function multicheck()
		{ 
			$this->load->view('multicheck_insert');
			if(isset($_POST['save']))
			{
				$user_id=1;/* Pass the userid here */
				$checkbox = $_POST['check']; 
				for($i=0;$i<count($checkbox);$i++){
					$category_id = $checkbox[$i];
					$this->Crud_model->multisave($user_id,$category_id);/* Call the modal */
					
				}
				echo "Data added successfully!";
			}
		}
		
	}
?>

Crud_model.php (Model)

<?php
class Crud_model extends CI_Model 
{
	function multisave($user_id,$category)
	{
		$query="insert into user_cat values($user_id,$category)";
		$this->db->query($query);
	}
	
}

insert.php (View)

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="">
	<input type="checkbox" id="checkItem" name="check[]" value="1">Car<br>
	<input type="checkbox" id="checkItem" name="check[]" value="2">Bike<br>
	<input type="checkbox" id="checkItem" name="check[]" value="3">Cycle<br>
	<button type="submit" class="btn btn-primary" style="width:200px" name="save">Submit</button>
</form>
</body>
</html>

Run the program on your browser with URL:

http://localhost/codeIgniter/index.php/Crud/multicheck

Here codeIgniter is my folder name. Put your folder name instead of codeIgniter.Rest of things are same.