How to use PDO to insert data into the database?

The SQL code for the users table:

1) Write a regular SQL query but, instead of values, put named placeholders. For example:
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:
3) Bind the placeholders to the variables:
You can add a third parameter which filters the data before it reaches the database:
  • 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.

5) Execute the query:
6) Check that the insertion really worked:
If the last inserted id is greater than zero, the insertion worked.
All code together now:
For localhost Database name is : pdoanuj
Sql file available inside the package.
[aio_button align=”none” animation=”none” color=”red” size=”small” icon=”play” text=”Demo” target=”_blank” relationship=”dofollow” url=””]        [aio_button align=”none” animation=”none” color=”red” size=”small” icon=”download” text=”Download” relationship=”dofollow” url=””]

Anuj Kumar

I'm Anuj kumar a web developer with 4+ year experience .I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Recommended Tutorials for you

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *