Hi guys this tutorial based on PHP Framework. Today in this tutorial we discuss on how to execute insert and update queries using model in CI.
We already created model - views -controller
Now follow the step :
Firstly we execute insert query in CI
Open home view and create form in html
Insert in CI
Now save this file in application->views->insert.php with .php extension.
Now Open Home controller and create new method named insert(); and load insert view in it.
public function insert(){$this->load->view("insert", $data);}
Now open browser and type http://localhost/CI/Home/insert in url. (if you getting error some thing like below : )
Now goto application->config open config.php
And replace
$config["base_url"] = ""; with this$config["base_url"] = "http://localhost/CI/"; then save it.
Now open autoload.php from config directory.
replace
$autoload["helper"] = array(""); with $autoload["helper"] = array("url"); this.
Now refresh browser and look something like :
Now open Controller Create another method named add();
public function add(){$data = $this->input->post();$result = $this->HomeModel->add($data);if($result){redirect(base_url()."Home/index?alert=success");}else{redirect(base_url()."Home/index?alert=failed_to_inserted");}}
Now Open Homemodel.php from models directory.
Create method add();
public function add($data){ $arr = array("name"=>$data["name"],"email"=>$data["email"], ); $this->db->insert("demo",$arr); return $this->db->insert_id(); }
All is done, Now enter name and email id then hit submit.
Now after redirection
If your screen looks like as above Image, its means you successfully done your JOB.
I hope you like this tutorial.
On next tutorial we will discuss on how to insert and update database in CodeIgniter.
0 comments