Select Insert Update Delete Record using PHP and MySQL

Select Insert Update Delete Record using PHP and MySQL

How to insert update delete record using php and mysql. In this tutorial we will show you step by step how to insert update delete record using php and mysql.

As well as you can use this insert update delete in php with source code to personally and professinally.

How to Select Insert Update Delete Record using PHP and MySQL

Use the following steps to select insert update delete record using PHP and MySQL:

  • DB.PHP – Connecting Database
  • Insert.PHP – Insert Records Into MySQL DB
  • Select.php – Select Record From MySQL DB
  • Update.php – Update Record Into MySQL DB
  • Delete.php – Delete Record From MySQL DB

In this example post will use the following SQL query to Select, insert, update, and delete records from MySQL Database Table in PHP.

  • In starting, we will create MySQL database connection file in PHP.
    • Will use mysqli_connect() function to connecting database with PHP.
  • Then will use “SELECT FROM” SQL query to select a record from the MySQL database.
  • Then “INSERT INTO” SQL query to insert record into the MySQL database.
  • After that, “Update” SQL query to update the record into the MySQL database.
  • Finally, “DELETE From” SQL query to delete record into MySQL database.
  • As well as will use mysqli_query() function for run sql queries.

1 – DB.PHP – Connecting Database

First of all, Create db.php file to connecting mysql database with PHP:

<?php
	$host='localhost';
	$username='root';
	$password='';
	$dbname = "mydb";
	$conn=mysqli_connect($host,$username,$password,"$dbname");
	if(!$conn)
        {
          die('Could not Connect MySql Server:' .mysql_error());
        }
?>

2 – Insert.PHP – Insert Records Into MySQL DB

Then, Create a new file named insert.php and add the following code into insert.php:

<?php
include_once 'db.php';

   
     $firstname = 'Hello';
     $lastname = 'Test';
     $mobile = '9874561230';

     $sql = "INSERT INTO users (firstname,lastname,mobile)
     VALUES ('$name','$email','$mobile')";

     if (mysqli_query($conn, $sql)) {
        echo "New record has been added successfully !";
     } else {
        echo "Error: " . $sql . ":-" . mysqli_error($conn);
     }
     mysqli_close($conn);

?>

Note that, The SQL INSERT statement will insert record in MySQL database using PHP function.

3 – Select.php – Select Record From MySQL DB

Then, create a new file named Select.php and add the following code into selete.php:

<?php 
include_once 'db.php';

$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 "Mobile = ".$data['mobile']."<br><hr>";
}
?>

Note that, The SQL SELECT statement will SELECT record FROM MySQL database using PHP function.

If you want to select limited records from MySQL database, you can use LIMIT clause with SQL query like following:

$sql = "SELECT * FROM users Limit 10";

Or, if you want to fetch record with ascending or descending order. So, you can use ASC OR DESC with SQL query.

4 – Update.php – Update Record Into MySQL DB

Now, you can modify database table records using SQL “UPDATE “ statement query. 

So, Create a new file named Update.php and add the following code into update.php:

<?php 

include_once 'db.php';

$sql = "UPDATE users SET firstname = 'Hello',lastname = 'Test',mobile = '9874561230' WHERE id=1 ";

$query = mysqli_query($conn,$sql);
if(!$query)
{
    echo "Query does not work.".mysqli_error($conn);die;
}
else
{
    echo "Data successfully updated";
}

  
?>

5 – Delete.php – Delete Record From MySQL DB

If you want to delete some records or all records from database, you can use DELETE SQL statement with PHP.

So, Create a new file named Delete.php and add the following code into delete.php:

<?php
	include_once 'db.php';

	$sql = "DELETE FROM users WHERE userid='" . $_GET["userid"] . "'";

	if (mysqli_query($conn, $sql)) {

	    echo "Record deleted successfully";

	} else {
	
	    echo "Error deleting record: " . mysqli_error($conn);
	}
	mysqli_close($conn);
?>

Recommended PHP Posts

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Select Insert Update Delete Record using PHP and MySQL

  1. Hi,
    First of all I would like to thank you for your tutorial. I am very happy with your tutorial. I need to know about laravel what can I do for that?

Leave a Reply

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