MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to use PDO to insert data into the database?

Banner with PDO logo titled How to use PDO to insert data into the database?

The SQL code for the users table:

[code language=”sql”]
CREATE TABLE IF NOT EXISTS users (id int(11) NOT NULL AUTO_INCREMENT,
name varchar(60) DEFAULT NULL,
phone varchar(12) DEFAULT NULL,
city varchar(60) DEFAULT NULL,
date_added date DEFAULT NULL,
PRIMARY KEY (id)
)
[/code]

1) Write a regular SQL query but, instead of values, put named placeholders. For example:

[code language=”php”]
$sql = "INSERT INTO `users`(`name`, `phone`, `city`, `date_added`)
VALUES(:name,:phone,:city,:date)";
[/code]

The use of placeholders is known as prepared statements. We use prepared statements as templates that we can fill later on with actual values.
2) Prepare the query:

[code language=”php”]
$query = $dbh -> prepare($sql);
[/code]

3) Bind the placeholders to the variables:

[code language=”php”]
$query->bindParam(‘:name’,$name);
[/code]

You can add a third parameter which filters the data before it reaches the database:

[code language=”php”]
$query->bindParam(‘:name’,$name,PDO::PARAM_STR);
$query->bindParam(‘:phone’,$phone,PDO::PARAM_INT);
$query->bindParam(‘:city’,$city,PDO::PARAM_STR);
$query->bindParam(‘:date’,$date,PDO::PARAM_STR);
[/code]

  • PDO::PARAM_STR is used for strings.
  • PDO::PARAM_INT is used for integers.
  • PDO::PARAM_BOOL allows only boolean (true/false) values.
  • PDO::PARAM_NULL allows only NULL datatype.

4) Assign the values to the variables.

[code language=”php”]
$name = "Amardeep Dubey";
$phone = "1234567890";
$city = "Bokaro";
$date = date(‘Y-m-d’);
[/code]

5) Execute the query:

[code language=”php”]
$query -> execute();
[/code]

6) Check that the insertion really worked:

[code language=”php”]
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId>0)
{
echo "OK";
}
else
{
echo "not OK";
}
[/code]

If the last inserted id is greater than zero, the insertion worked.
All code together now:

[code language=”php”]
$sql = "INSERT INTO `users`
(`name`, `phone`, `city`, `date_added`)
VALUES
(:name,:phone,:city,:date)";
$query = $dbh -> prepare($sql);
$query->bindParam(‘:name’,$name,PDO::PARAM_STR);
$query->bindParam(‘:phone’,$phone,PDO::PARAM_INT);
$query->bindParam(‘:city’,$city,PDO::PARAM_STR);
$query->bindParam(‘:date’,$date);
// Insert the first row
$name = "Amardeep Dubey";
$phone = "1234567890";
$city = "Bokaro";
$date = date(‘Y-m-d’);
$query -> execute();
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId>0)
{
echo "OK";
}
else {
echo "not OK"; }
[/code]

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Introduction to PHP Data Object PDO in PHP

There are three main API options when considering connecting to a MySQL database server. PHP’s MySQL Extension. PHP’s mysqli Extension. PHP Data Object (PDO). PHP Data Object…

Read More

Complete reference guide of PHP Databases!

PHP MySQL Introduction MySQL is one of the most popular relational database system being used on the Web today. It is freely available and easy to install, however if…

Read More

How to delete records?

1) Write the delete statement: [code language=”php”] $sql = "DELETE FROM `users` WHERE `id`=:id"; [/code] 2) Prepare the query: [code language=”php”] $query = $dbh -> prepare($sql); [/code]…

Read More

How to use PDO to update the database?

1) Write the regular update statement and again, instead of values, assign the named placeholders. For example: [code language=”php”] $sql = "UPDATE `users` SET `city`= :city, `phone`…

Read More

How to use PDO to read data from the database?

1) Write the regular select statement and again, instead of values, put named placeholders. For example: [code language=”php”] $sql = "SELECT * FROM users"; [/code] 2) Prepare…

Read More

PDO – PHP database extension

PDO – PHP database extension PDO (PHP Data Objects) is a PHP extension through which we can access and work with databases. Though PDO is similar in…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x