Inserting Data into the Database
Learn how to insert data into the database using PHP PDO.
In this lesson, we’ll learn the different phases of inserting data into the database. We’ll see how to write queries, bind placeholders, assign values to variables, and finally execute them.
Step 1: Write a query
Write a regular SQL query but, instead of values, put named placeholders. For example:
$sql = "INSERT INTO `users`
(`name`, `phone`, `city`,
`date_added`)
VALUES
(:name,:phone,:city,:date)";
The use of placeholders is known as prepared statements. We use prepared statements as templates that we can later fill with actual values.
Step 2: Prepare the query
$query
...