insert Query In Codeigniter Example Tutorial

Today, We want to share with you insert Query In Codeigniter Example Tutorial.In this post we will show you update query in codeigniter, hear for codeigniter update query we will give you demo and example for implement.In this post, we will learn about codeigniter delete query with an example.

insert Query In Codeigniter Example Tutorial

There are the Following The simple About delete in codeigniter Full Information With Example and source code.

As I will cover this Post with live Working example to develop select query in codeigniter, so the codeigniter join query is used for this example is following below.

insert Query In Codeigniter Example Tutorial
insert Query In Codeigniter Example Tutorial

PHP Based Framework like as a CodeIgniter insert or added store query will run or execute using following all the functions. They are

  • Insert With Query Bindings
  • Standard Insert
  • Escaping Insert Queries
  • Retrive Inserted ID
  • Get Affected Rows
  • $this->db->query()
  • $this->db->insert_string()
  • $this->db->insert_batch()

Codeigniter (CRUD) Select Insert Update Delete Query

  1. Update Query In Codeigniter
  2. Select Query In Codeigniter
  3. Insert Query In Codeigniter
  4. Delete Query In Codeigniter

using $this->db->query()

Example 1:

$query = "insert into tbl_member (first_name, price, teamfirst_name)
        values ('pakainfo, 35, 'Infinityknow 1')";
$this->db->query($query);

Example 2:

$data = array( 
        'first_name'	= >  $_POST['first_name'] , 
        'teamfirst_name'= >  $_POST['teamfirst_name'], 
        'price'	= >  $_POST['price']
    );
$this->db->insert('tbl_member', $data);

Insert With Query Bindings

Use Of Query Bindings

Benefit of using binds is that the values are automatically escaped, producing safer queries

$query = "insert into tbl_member (first_name, price, teamfirst_name)
        values (?, ?, ?)";
$this->db->query($query,array('pakainfo, 35, 'Infinityknow 1'));

Standard Insert


$query = "INSERT INTO tbl_member (first_name, teamfirst_name, price) 
        VALUES (".$this->db->escape($first_name).", ".$this->db->escape($teamfirst_name).".", ".$this->db->escape($price).")";
$this->db->query($query);


using $this->db->insert_string()

Note: Values are automatically escaped, producing safer queries.

$data = array( 
        'first_name'	= >  $_POST['first_name'] , 
        'teamfirst_name'= >  $_POST['teamfirst_name'], 
        'price'	= >  $_POST['price'] 
    );
$this-> db->insert_string('tbl_member', $data);

using $this->db->insert_batch()

$data = array(
            array(
                'first_name'	= >  'first_name1' , 
                'teamfirst_name'= >  'teamfirst_name1', 
                'price'	= >  'price1'
            ),
            array(
                'first_name'	= >  'first_name2' , 
                'teamfirst_name'= >  'teamfirst_name2', 
                'price'	= >  'price2'
            )
        );
$this->db->insert_batch('tbl_member', $data); 
//INSERT INTO mytable (first_name, teamfirst_name, price) 
//VALUES ('first_name1', 'teamfirst_name1', 'price1'), ('first_name2', 'teamfirst_name2', 'price2')

Escaping Insert Queries

$this->db->escape()

This function determines the data type so that it can escape only string data. It also automatically adds single quotes around the data so you don’t have to:

$query = "INSERT INTO tbl_member (first_name) VALUES(".$this->db->escape($first_name).")";


using $this->db->escape_str()

This function escapes the data passed to it, regardless of type. Most of the time you’ll use the above function rather than this one. Use the function like this:

$query = "INSERT INTO tbl_member (first_name) VALUES('".$this->db->escape_str($first_name)."')";


Get Last Inserted ID

The Get the Last insert ID number when performing CodeIgniter database Last row inserts.

$this->db->insert_id()

Get Affected Rows

show the number of affected records, when doing “write” type mysql queries Like as a CRUD Operations (insert, update, etc.).

$this->db->affected_rows();

How to insert data in database – CodeIgniter framework

simple tou can also The INSERT INTO query is used to insert or store sabe new fresh data to a MySQL Database table:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

codeIgniter\application\controllers\Member.php
<?php 
class Member extends CI_Controller 
{
	public function __construct()
	{
	parent::__construct();
	
	$this->load->database();
	
	$this->load->model('Member_model');
	}
	public function savedata()
	{
		$this->load->view('insert');
	
		if($this->input->post('save'))
		{
		
		$member_user=$this->input->post('member_user');
		$sirname=$this->input->post('sirname');
		$email=$this->input->post('email');
		
		$this->Member_model->saverecords($member_user,$sirname,$email);	
		echo "Member all the Records Saved Successfully";
		}
	}
	
}
?>

codeIgniter\application\models\Member_model.php

<?php
class Member_model extends CI_Model 
{
	function saverecords($member_user,$sirname,$email)
	{

	$query="insert into crud values('','$member_user','$sirname','$email')";
	$this->db->query($query);
	}
	
}

codeIgniter\application\views\insert.php

<!DOCTYPE html>  
<html>
<head>
<title>Registration form - www.pakainfo.com</title>
</head>
 
<body>
	<form method="post">
		<table width="600" border="1" cellspacing="5" cellpadding="5">
  <tr>
    <td width="230">User(Member) Name </td>
    <td width="329"><input type="text" name="member_user"/></td>
  </tr>
  <tr>
    <td>Sirname </td>
    <td><input type="text" name="sirname"/></td>
  </tr>
  <tr>
    <td>Email ID </td>
    <td><input type="email" name="email"/></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="submit" name="save" value="Save Data"/></td>
  </tr>
</table>
 
	</form>
</body>
</html>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about join query in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   jQuery Print Dynamic Content using PHP MySQL Example
  2.   Laravel 6 Eloquent WHERE Like query Example
  3.   ng-table Sorting using Angular Example
  4.   javascript Manage cookies setcookie and getcookie
  5.   WordPress AJAX - Wordpress jQuery AJAX POST Request
  6.   Count Query Codeigniter Tutorial
  7.   join query in codeigniter Example Tutorial
  8.   Get Content and Attributes using JavaScript jQuery
  9.   Update Query In Codeigniter Example Tutorial
  10.   select Query In Codeigniter Example Tutorial