Insert, Update and Delete Operations in MySQLi Database using PHP

Insert, Update, and Delete Operations in MySQLi Database using PHP

In databases, we normally have four types of operations which are also known as CRUD operation (create, retrieve, update and delete). We have already discussed in our previous posts about how to create a connection to database in PHP and how to retrieve values from database today in this post we cover the update, delete and insert operation. Previously we learn to create a connection to MYSQL.

First to insert values, we must have a table where we insert values so suppose we have a table which we already used in the last post named city_info which contains the city name the province of that city and the total population in that city.

Insert Operation:

As we have the following table schema.

 

To insert new values in that table first thing we have to remember is to check its integrity so as we have city_id in this table which is the primary key and automatically increment whenever a new row is inserted so while inserting a new row we ignore this column or put it null. So the SQL insert query is following:

 

To run this query in PHP, we have to store this query, in any string like $sql_query.

$conn->query ($sql_query); is used to run that query and this function return true or false as a result.

 

Update Operation:

In update query, we have to pass new values of a table, and we limit the update operation using where clause. If we not do that it will update all the rows in a table.

 

 

Delete Operation:

Delete operation is used to delete some data from the database.

You can also retrieve data from MYSQL database.