Turn Your Vehicle Into a Smart Earning Asset

While you’re not driving your car or bike, it can still be working for you. MOTOSHARE helps you earn passive income by connecting your vehicle with trusted renters in your city.

🚗 You set the rental price
🔐 Secure bookings with verified renters
📍 Track your vehicle with GPS integration
💰 Start earning within 48 hours

Join as a Partner Today

It’s simple, safe, and rewarding. Your vehicle. Your rules. Your earnings.

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]

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x