Insert, Edit, Update, Delete Data using Php

In order to insert, edit, update Data in Php, We need to follow certain steps:-

  1. Check the connection to database.
  2. Create an html Form
  3. Insert data to database by filling form
  4. Edit, Update, Delete Data.

Check the connection to database.

In order to check connection to database, we need to know SERVER NAME, USER, PASSWORD, DATABASE .

db.php

<?php
ob_start();
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'techalltype');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>

NOTE:- While working on local computer server name is localhost, username is root,and password is blank (by default).

Create an html Form:-

In this step we simply use html to create a form.

index.php

<form action="insert.php" method="post">
<p>
  <label> USER NAME : </label>
  <input type="text" name="username" required placeholder="Enter your name" />
</p>
<p>
  <label> EMAIL ID : </label>
  <input type="email" name="usermail" required placeholder="Enter your Email ID"/>
</p>
<p>
  <label> MOBILE NUMBER : </label>
  <input type="text" name="usermobile" required placeholder="Enter your mobile number"  />
</p>
<p>
  <input type="submit" name="send" value="Submit" />
</p>
</form>

Here action=”insert.php”  means when we click on submit button the page will redirect to insert.php page. Method=”post” means when form will submit form will use post data.(Post data is referred most times because post method can carry maximum data from one page to another).

Insert.php

<?php
 ob_start();
  include("db.php");  // check the connection
  if(isset($_POST['send'])!="") // if send button is clicked
  {
  $username=mysql_real_escape_string($_POST['username']);  // mysql_real_escape_string use to escapes special characters in a string
  $usermail=mysql_real_escape_string($_POST['usermail']);
  $usermobile=mysql_real_escape_string($_POST['usermobile']);
  $update=mysql_query("INSERT INTO register(username,emailid,mobileno,created)VALUES
									  ('$username','$usermail','$usermobile',now())"); // insert statement to add data to database
  
  if($update)
  {
	  $msg="Successfully Updated!!";
	  echo "<script type='text/javascript'>alert('$msg');</script>";// If data inserted to database alert message display
	  header('Location:index.php');// If data inserted move to index.php page 
  }
  else
  {
	 $errormsg="Something went wrong, Try again"; 
	  echo "<script type='text/javascript'>alert('$errormsg');</script>"; // if data not inserted up line message display
	  header('Location:index.php'); If data not inserted move to index.php page 
  }
  }
 ob_end_flush();
?>

Display data from database:-

To view data from database, we need to create a page

view.php

<?php
 include('db.php');
  $select=mysql_query("SELECT * FROM register order by id desc"); // select * means display all records 
  $i=1;
  while($userrow=mysql_fetch_array($select))
  
  {
  $id=$userrow['id'];
  $username=$userrow['username'];
  $usermail=$userrow['emailid'];
  $usermobile=$userrow['mobileno'];
  $created=$userrow['created']
?>

 <p> USER NAME : <span><?php echo $username; ?></span>
    <a href="delete.php?id=<?php echo $id; ?>" 
    onclick="return confirm('Are you sure you wish to delete this Record?');">
    		<span class="delete" title="Delete"> delete </span></a>
  </p>
  <br />
  <p> EMAIL ID : <span><?php echo $usermail; ?></span>
    <a href="edit.php?id=<?php echo $id; ?>"><span class="edit" title="Edit"> edit </span></a>
  </p>
  <br />
  <p> MOBILE NO : <span><?php echo $usermobile; ?></span>
  </p>
<br>
<?php } ?>

Edit data-

In order to edit data from database we need to click on edit button available on view.php.

edit.php

<?php 
ob_start();
include('db.php');
if(isset($_GET['id']))
{
  $id=$_GET['id'];
  if(isset($_POST['update']))
  {
  $eusername=$_POST['eusername'];
  $eusermail=$_POST['eusermail'];
  $emobile=$_POST['eusermobile'];
  $updated=mysql_query("UPDATE register SET 
		username='$eusername', emailid='$eusermail', mobileno='$emobile' WHERE id='$id'")or die();
  if($updated)
  {
  $msg="Successfully Updated!!";
  header('Location:index.php');
  }
}
}
ob_end_flush();
?>


<?php 
  if(isset($_GET['id']))
  {
  $id=$_GET['id'];
  $getselect=mysql_query("SELECT * FROM register WHERE id='$id'");
  while($profile=mysql_fetch_array($getselect))
  {
    $username=$profile['username'];
    $usermail=$profile['emailid'];
    $usermobile=$profile['mobileno'];
?>

<form action="" method="post">
    <p>
      <label> USER NAME : </label>
      <input type="text" name="eusername" required placeholder="Enter your name" 
      value="<?php echo $username; ?>" id="inputid" />  // calling data from database in textbox and by submitting the form updated data insert in database
    </p>
    <p>
      <label> EMAIL ID : </label>
      <input type="email" name="eusermail" required placeholder="Enter your Email" 
      value="<?php echo $usermail; ?>" id="inputid" />
    </p>
    <p>
      <label> MOBILE NUMBER : </label>
      <input type="text" name="eusermobile" required placeholder="Enter your mobile number" 
      value="<?php echo $usermobile; ?>" id="inputid" />
    </p>
    <p>
      <input type="submit" name="update" value="Update" id="inputid" />
    </p>
  </form>

<?php } } ?>

Delete data-

In order to delete data from database using form we make page

delete.php

<?php
  ob_start();
  include("db.php");
  if(isset($_GET['id'])!="")
  {
  $delete=$_GET['id'];
  $delete=mysql_query("DELETE FROM register WHERE id='$delete'");
  if($delete)
  {
	  header("Location:index.php");
  }
  else
  {
	  echo mysql_error();
  }
  }
  ob_end_flush();
?>

Register.sql

CREATE TABLE IF NOT EXISTS `register` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(500) NOT NULL,
  `emailid` varchar(500) NOT NULL,
  `mobileno` varchar(500) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

 

 

 

 

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , , , ,