Monday, October 12, 2015

Select Insert Update Delete In PHP MySql


PHP-MySql



We want Create, Insert, Update, Delete on the same page.ie we enter the data in the form and that data will displayed in tabular form on same page.
There are available option such as Save, Edit, Delete in front of them.
If we click on Save button the data will be saved and displayed in the table, to change any field we click on the Edit and if we want to delete any row the we click on the Delete.

Insert Data
Records

STEP-1:Crete Config.php file.

<?php

    error_reporting(1);

    $host="localhost";//Server Name

    $username="root";//User Name

    $pass="";//Password

    $db="blog_demo";//Database Name

    $connect=mysql_connect($host,$username,$pass)or die(mysql_error());//Connect to MySql Server

    $dbselect=mysql_select_db($db,$connect)or die(mysql_error());//Select Database

?>
STEP-2:Create index.php file.

PHP SCRIPT GET THE VALUE FROM HTML FORM 

<?php
include 'config.php';
if(isset($_POST['submit'])&&$_POST['submit']=="Save")
{
    if($_POST['oldid']!='')//Code for Update Record
    {    $id=$_POST['oldid'];
        $sql="UPDATE `info` SET `name` = '".$_POST['name']."', `email` ='".$_POST['email']."', `address` = '".$_POST['address']."', `mobile` = '".$_POST['mobile']."' WHERE `id` = '".$id."'";
        $query=mysql_query($sql);
        header('location:index.php');//redirect to index page
        exit;
    }
    else//Code for Insert New Record
    {
        $sql="INSERT INTO `info`(name,email,address,mobile)VALUES('".$_POST['name']."','".$_POST['email']."','". $_POST['address']."','".$_POST['mobile']."')";
        $query=mysql_query($sql);
        header('location:index.php');//redirect to index page
        exit;
    }
}
if(isset($_GET['action'])&&$_GET['action']=="edit")//Code for edit the record
{
    $id=$_GET['id'];
    $sql_edit="SELECT * FROM `info` WHERE id='".$id."'";
    $query=mysql_query($sql_edit);
    $fetch_record=mysql_fetch_array($query);
    $name=$fetch_record['name'];
    $email=$fetch_record['email'];
    $address=$fetch_record['address'];
    $mobile=$fetch_record['mobile'];
}    
if(isset($_GET['action'])&& $_GET['action']=='delete')//Code For Delete Record
{
    $id=$_GET['id'];
    $del = "DELETE FROM `info` WHERE `id` = ".$_GET['id'];
    $success=mysql_query($del);
    header('location:index.php');
    exit;
}
?>

HTML FORM 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" />
<title>Insert-Update-Delete in PHP-MySql</title>
</head>
<body>
<form method="post" name="insert" id="insert">
  <center>
    <h2>Insert Data</h2>
  </center>
  <center>
  <table class="table1">
        <input type="hidden" name="oldid" value="<?php echo $id; ?>" />
    <tr>
      <td>Name:</td>
      <td><input class="txt" type="text" id="name" name="name" value="<?php echo isset($name)?$name:''; ?>" /></td>
    </tr>
    <tr>
      <td>Email:</td>
      <td><input type="email" class="txt" id="email" name="email" value="<?php echo isset($email)?$email:''; ?>" /></td>
    </tr>
    <tr>
      <td>Address:</td>
      <td><textarea rows="4" class="txt" name="address" id="address"><?php echo isset($address)?$address:''; ?></textarea></td>
    </tr>
    <tr>
      <td>Mobile:</td>
      <td><input type="text" class="txt" name="mobile" id="mobile"  value="<?php echo isset($mobile)?$mobile:''; ?>"/></td>
    </tr>
  </table>
  <input type="submit" class="submit" name="submit" value="Save" />
</form>
</center>
<center>
<div class="container">
  <h2>Records</h2>
  <table class="table2" border="1">
    <tr>
      <td>Name</td>
      <td>Email</td>
      <td>Address</td>
      <td>Mobile</td>
      <td>Action</td>
    </tr>
    <?php
    $select=mysql_query("SELECT * FROM `info`");
    while($row=mysql_fetch_array($select)){
 ?>
    <tr>
      <td><?php echo $row['name']; ?></td>
      <td><?php echo $row['email']; ?></td>
      <td><?php echo $row['address']; ?></td>
      <td><?php echo $row['mobile']; ?></td>
      <td><a href="index.php?action=edit&id=<?php echo $row['id']; ?>">Edit</a> | <a href="index.php?action=delete&id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure?')">Delete</a>
        <?php } ?>
  </table>
  </center>
</div>
</body>
</html>
In the given Example user enter the data in the form and click on Save button.
Record is stored in MySql Database and displayed in the index page in tabular form.
With all row a Delete and Edit link will be displayed.
Whenever user want to update he has to click on edit link or can click on delete link to delete the information.


CLICK HERE FOR DOWNLOAD COMPLETE CODE...

0 comments:

Post a Comment