Skip to main content
Select Insert Update Delete in PHP MySQL

Select Insert Update Delete in PHP MySQL code example

In the previous tutorial we saw that how to connect MySQL database with PHP. We created a database and table in PHPMYADMIN using MySQL and PHP. In this tutorial we will learn that how to “Select Insert Update Delete in PHP MySQL”. Select data from database, insert data in database, update data and delete data are common process for every web-application. You can do many thing with MySQL database using PHP but here we learn about this basic processes. So let’s start step by step…

Important for you : Login and logout example using session in PHP

Important for you : Send email from localhost XAMPP in PHP

Insert data into MySQL in PHP

Kindly refer the previous tutorial because this is next part of previous tutorial. In the previous tutorial, we started working on “project” folder and created one “config.php” file for database connection. Now for this tutorial create a new file named “insert.php” in “project” folder and copy – paste below code inside it.

insert.php

<?php 
include 'config.php';	//Database configuration file
mysqli_select_db($conn,$db_name);
$sql = "INSERT INTO users (id,firstname,lastname,email) VALUES (NULL,'Om','Dayal','Om@xmail.com')";
$query=mysqli_query($conn, $sql);
if(!$query)
{
	echo "User does not inserted.".mysqli_error($conn);
}
else
{
	echo "User inserted successfully.";
}
?>

Now open your browser and locate this link “localhost/project/insert.php”. You will see message like “User inserted successfully.”.

Description:
In the above example,

  • First we connect MySQL with PHP using “config.php” file.
  • If you are working with many database then you can use mysqli_select_db() function for select database for particular operation.
  • Then we use “INSERT INTO” syntax query of MySQL language for insert data into database. After that we use mysqli_query() function for run MySQL syntax query.
  • Finally use if…else… condition to check data is inserted or not.

Select data from MySQL in PHP

Let’s get back recently inserted data by above program. For select data from database using MySQL and PHP. Let’s create one new file “select.php”. Copy the below code and paste it inside “select.php” file.

. Now open browser and navigate path name “localhost/project/select.php”. You will find out users name and detail.

select.php

<?php 
include 'config.php';
mysqli_select_db($conn,$db_name);
$sql = "SELECT * FROM users";
$query = mysqli_query($conn,$sql);

if(!$query)
{
	echo "Query does not work.".mysqli_error($conn);die;
}

while($data = mysqli_fetch_array($query))
{
	echo "Id = ".$data['id']."<br>";
	echo "Firstname = ".$data['firstname']."<br>";
	echo "Lastname = ".$data['lastname']."<br>";
	echo "Email = ".$data['email']."<br><hr>";
}
?>

Result:

Id = 1
Firstname = Om
Lastname = Dayal
Email = Om@xmail.com

Update data in mysql using php

Let’s change or update database record info by using “UPDATE DATA” query syntax of MySQL. Remember that you can not update single record without any reference field. Here we use ID field as reference field to update the record. See the below example. Create “update.php” and copy the below code and paste it inside “update.php” file.

update.php

<?php 
include 'config.php';
mysqli_select_db($conn,$db_name);
$sql = "UPDATE users SET firstname = 'OM_new',lastname = 'Dayal_new',email = 'om@new.com' WHERE id=1 ";
$query = mysqli_query($conn,$sql);

if(!$query)
{
	echo "Query does not work.".mysqli_error($conn);die;
}
else
{
	echo "Data successfully updated";
}
?>

Now open your browser and locate path “localhost/product/update.php”. If you find message like this “Data successfully updated”, then your record is update successfully otherwise you have error in MySQL syntax or something else.

Delete data in mysql using php

If there are many unwanted data placed in database then you can delete them by “DELETE” MySQL syntax. Here also you want any reference field to delete data. See the below example for more description.

Create a “delete.php” file, copy the below code and paste it inside delete.php file.

Delete.php

<?php 
include 'config.php';
mysqli_select_db($conn,$db_name);
$sql = "DELETE FROM users WHERE id = 1";
$query = mysqli_query($conn,$sql);

if(!$query)
{
	echo "Query does not work.".mysqli_error($conn);die;
}
else
{
	echo "Data successfully delete.";
}
?>

Now open your browser and locate the path “localhost/project/delete.php”. If you find message like this “Data successfully delete.”. Then you have done it. Your data is deleted from database. Check “Users” table in PHPMYADMIN.

I hope that you like this Select Insert Update Delete in PHP MySQL code tutorial. Please share it with your friends and circle. If you have any questions while performing this tutorials then please let me know in comment. I will surely help you. Thank you very much.

admin

HI, i am software developer having knowledge of PHP, C, C++ programming languages. I am very passionate about programming and ability to solve the programming problems. CodeandTuts is my blog where i like to put new tutorial and help new developer.

3 thoughts on “Select Insert Update Delete in PHP MySQL code example

Leave a Reply

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