The following code example shows Inserting Records in a Database Table Using an Array of Objects in PHP.
Because we want to insert several records at a time, so we create an array of objects for it. At first, we insert rows in the course table. So, we create a class with the name an_class containing data members corresponding to the course table. So, the an_class has four data members cid, cname, duration, and credits corresponding to the fields of the course table course_id, course_name, duration, and credits respectively. Also, the class an_class has a constructor that initializes the four data members.
After that we must create an array of objects of the an_class with each element of the array contains the values that are to be inserted as a row. Once we have the data that we are going to insert in the table, we need to create a connection with the database. Then we create a string with an insert command that represents a prepared statement. Since we need to execute a number of insert commands in a loop, the prepared statement is desirable.
Further, we start a loop that runs for the number of elements in the array. The loop will bind the parameters and executes the insert statement in each iteration.
Complete Program for Inserting Records
<?php
// Creating a class that wil hold the values to be inserted
class an_class{
public $cid, $cname, $duration, $credits;
function __construct($c, $n, $d, $r)
{
$this->cid=$c;
$this->cname=$n;
$this->duration=$d;
$this->credits=$r;
}
}
// creating an array of objects
$course_array=array(new an_class(11, 'Web Development with Python-Django', 4, 20),
new an_class(12, 'MERN Stack Development', 3, 20),
new an_class(13, 'Pyhon Programming', 2, 10),
new an_class(14, 'MEAN Stack Development', 3, 20),
new an_class(15, 'Cloud Computing with AWS', 6, 40));
// The values that will be inserted
foreach($course_array as $c)
{
echo '[', $c->cid, ' ', $c->cname, ' ', $c->duration, ' ', $c->credits, ']<br>';
}
$server_host="localhost";
$database_user="root";
$password="";
// create a new connection with the database
$con1=new mysqli($server_host, $database_user, $password, "institute");
// Formulate the insert command
$query="insert into course(course_id, course_name, duration, credits)values(?,?,?,?)";
$s=$con1->prepare($query);
//Bind parameters in a for loop
foreach($course_array as $c){
$s->bind_param("isii", $x, $y, $z, $w);
$x=$c->cid;
$y=$c->cname;
$z=$c->duration;
$w=$c->credits;
// Execute the query
$s->execute();
echo 'Record Inserted Successfully!';
}
$s->close();
$con1->close();
?>
Output
Similarly, we can insert some rows in the trainee table. The following code demonstrates it.
<?php
// Creating a class that wil hold the values to be inserted
class trainee{
public $tid, $tname, $location, $credits;
function __construct($c, $n, $d)
{
$this->tid=$c;
$this->tname=$n;
$this->location=$d;
}
}
// creating an array of objects
$trainee_array=array(new trainee(101, 'Arther', 'New York'),
new trainee(102, 'Ekansh', 'Fatehpur'),
new trainee(103, 'Sunidhi', 'Udupi'),
new trainee(104, 'Mili', 'Selam'),
new trainee(105, 'Sunak', 'Seol'),
new trainee(106, 'Schaum', 'Irpin'),
new trainee(107, 'Serhei', 'Sumy'),
new trainee(108, 'Scholl', 'Berlin'),
new trainee(109, 'Olaf', 'Madrid'),
new trainee(110, 'Inza', 'Ghazipur'),
new trainee(111, 'Palak', 'Ratlam'));
// The values that will be inserted
foreach($trainee_array as $c)
{
echo '[', $c->tid, ' ', $c->tname, ' ', $c->location, ']<br>';
}
$server_host="localhost";
$database_user="root";
$password="";
// create a new connection with the database
$con1=new mysqli($server_host, $database_user, $password, "institute");
// Formulate the insert command
$query="insert into trainee(trainee_id, tname, location)values(?,?,?)";
$s=$con1->prepare($query);
//Bind parameters in a for loop
foreach($trainee_array as $c){
$s->bind_param("iss", $x, $y, $z);
$x=$c->tid;
$y=$c->tname;
$z=$c->location;
// Execute the query
$s->execute();
echo 'Record Inserted Successfully!<br>';
}
$s->close();
$con1->close();
?>
Output
The database table would like like it after executing the above PHP script.
Further Reading
Examples of Array Functions in PHP