Thursday, October 12, 2017

Anil kr Prajapati

How To Insert Records Using Stored Procedure in MySql with PHP ?

If you want to insert large records in a table so it is great to do that by using of Stored Procedure. There are number of benefits to insert record with the use of Stored Procedure

  • Stored procedure cached on server so the speed is more fast.
  •  A Stored Procedures will be in one place so that there’s no confusion of having business rules spread over potentially disparate code files in the project.
  • More secure because there is no direct access with tables in database.
I am describing the process of inserting records in database by using Stored Procedure as below.

Step #1 : Create a Stored Procedure in your database.
you can create a Stored Procedure by two way.

1. Create a stored procedure by using of phpMyAdmin.

2. Create a Stored Procedure by using MySql query.

you can use below query to create a stored procedure to insert data.

CREATE  PROCEDURE `insert_records`(IN `name` VARCHAR(50))
insert into r (name) values (name);

Step #2 : Now Create table in which we want to insert data.

create table by using below query -

CREATE TABLE `insert_records`(`name` VARCHAR(100))

Step #3 : Call Stored Procedure by PHP script to insert names.

you can call a stored procedure by using below query to insert data -

     $name = array('anil','raj','sonia','monica','sivani');
    foreach($name  as $key=>$val){ 
$data = implode("','",$val);
echo '===== Standard data insertion start ===== <br/>'.$data.'<br/>';
$query = "CALL insert_records('".$data."')";
            $success = $conn->query($query);
                echo "inserted succesfully";             
                    echo "Data not successfully Inserted.";
catch(PDOException $e)
echo $e->getMessage();

When you run the script it will insert the all data in the table and if there is any error it will show exception.

you can easily insert data into database by using this process.

About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about PHP programming. Here you will find a hug of information on web development, web design, PHP and Latest technology.

Subscribe to this Blog via Email :