Tutorial - 4 | CodeIgniter PHP Framework

By Harsh Aggrawal | Jul, 20 2018 07:51

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 : <img src="https://codeigniter.com/assets/images/ci-logo-big.png" style="float:right" />
  • Firstly we execute insert query in CI
  • Open home view and create form in html
<html> <head> <title>Insert in CI </title> </head> <body> <form action="<?php echo base_url(); ?>Home/add" method="post"> <h1>Enter student detail</h1> <div>Student Name</div> <input type="text" name="name"> <br> <div>Student Email Id</div> <input type="text" name="email"> <br/> <input type="submit" value="submit"> </form> </body> </html>
  • 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.

Ask question and get answer MyinboxHub - The Tutorial Points</h3> <h3>Thanks for reading, for more details go through MyinboxHub - The Tutorial Points</h3> <xmp>


0 comments

No comment posted yet

Leave a comment