RunCodes
Programming & Tech

How to Insert, Delete, Update and Search Data in MySQL Database Using PHP?[With Source Code]

6 7,034

Hello guyz, if you are searching for tutorial which show inserting, deleting updating and searching data in your MySQL database through PHP script, then you are in the right article. In this tutorial article, we are gonna talk about it. This is the most basic operation in any programming language and this rule applies to PHP too. Without knowing this basic step, you can not develop either small or big project using PHP. So lets start.

First of all you need to have any server application installed in your PC. Without server application you can not execute any php file because PHP is server side scripting language. So, to make your PC as a server you need to install server application. I am using Wamp Server but you can use any server application software.

Download Wampserver: http://www.wampserver.com/en/

STEPS:

[sociallocker]

1. After installing this application, Start your wampserver, then open your localhost, for this open up your any browsing software and type localhost or 127.0.0.1 in the address bar. Then new page will appear then scroll down and find phpmyadmin and click on that. Then you will be able to see the login page. Login in by entering username and password (Default username is root and password is empty). And then click on new and create one database having any name you want and the click ok, then create table having any number of field you want but i am using four field in this case and click on go. After that, you need to insert the field name like: Roll No, Name, Address etc. and then select the length as well as data type as your requirement and then click on save. You can watch the video for this designing part, you may understand better in video. Make sure that make any one filed primary key, generally Roll No is set as PK because of unique identification than other field.

2. Open your any text editing software, i am using Dreamweaver CC, and then define site and testing server for your Dreamweaver cc, see this article.

3. After completing this task, write the following piece of code and save it in .php extension.


<!doctype html>
<?php
$servername = "localhost";
$username="root";
$password="";
$dbname="idusnew";
$Rollno="";
$fname="";
$lname="";
$address="";
$email="";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[0]=$_POST['Rollno'];
$data[1]=$_POST['fname'];
$data[2]=$_POST['lname'];
$data[3]=$_POST['address'];
$data[4]=$_POST['email'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM idusnew WHERE Rollno = '$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$Rollno = $rows['Rollno'];
$fname = $rows['fname'];
$lname = $rows['lname'];
$address = $rows['address'];
$email = $rows['email'];

}
}else{
echo("no data are available");
}
} else{
echo("result error");
}

}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `idusnew`(`fname`, `lname`, `address`, `email`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("data inserted successfully");

}else{
echo("data are not inserted");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
//delete
if(isset($_POST['delete'])){
$info = getData();
$delete_query = "DELETE FROM `idusnew` WHERE Rollno = '$info[0]'";
try{
$delete_result = mysqli_query($conn, $delete_query);
if($delete_result){
if(mysqli_affected_rows($conn)>0)
{
echo("data deleted");
}else{
echo("data not deleted");
}
}
}catch(Exception $ex){
echo("error in delete".$ex->getMessage());
}
}
//edit
if(isset($_POST['update'])){
$info = getData();
$update_query="UPDATE `idusnew` SET `fname`='$info[1]',lname='$info[2]',address='$info[3]',email='$info[4]' WHERE Rollno = '$info[0]'";
try{
$update_result=mysqli_query($conn, $update_query);
if($update_result){
if(mysqli_affected_rows($conn)>0){
echo("data updated");
}else{
echo("data not updated");
}
}
}catch(Exception $ex){
echo("error in update".$ex->getMessage());
}
}

?>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form method ="post" action="idusnew.php">
<input type="number" name="Rollno" placeholder="Roll No" value="<?php echo($Rollno);?>"><br><br>
<input type="text" name="fname" placeholder="First Name" value="<?php echo($fname);?>"><br><br>
<input type="text" name="lname" placeholder="Last Name" value="<?php echo($lname);?>"><br><br>
<input type="text" name="address" placeholder="Address" value="<?php echo($address);?>"><br><br>
<input type="text" name="email" placeholder="example@example.com" value="<?php echo($email);?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="delete" value="Delete">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">

</div>
</form>
</body>
</html>

4. Open this .php file in any browser you want. 🙂
[/sociallocker]

That’s it guyz. If you have any confusion, you can watch the video.

 

 

Show Comments (6)