In this tutorial, we will show you step by step how to develop an insert update delete records application 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
Here are steps to develop a simple application to select insert update, and delete table data from the database in php 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
# 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()); } ?>
# 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.
# 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.
# 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"; } ?>
# 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); ?>
Conclusion
That’s it, You have learned how to insert, view, edit and delete records from a database using PHP and MySQL.
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?