Friday, June 12, 2015

PHP Insert Update Delete Form With MySQL DB

hey coders.

In this post I'm going to show you a very basic PHP form with Insert Update Delete View actions connected to MySQL DB.
I've installed Wamp 2.5 on my PC.

1.Create connect.php to check connection with MySQL.

connect.php
_____________________________________________________________________________________

<?php

     //to check connection run this file
     $link = mysqli_connect('localhost', 'root', '', 'test');

     if ($link)
     {
         echo ("Connection is succeed");
     }
     else
     {
         echo ("Connection is fail");
     }

?>
___________________________________________________________________________________


2. Create input.php file
    important lines shown in blue.

 input.php
___________________________________________________________________________________

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Form Input Data</title>
</head>

<body>
<table border="1">
  <tr>
    <td align="center">Form Input Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
        <form method="post" action="insert.php">
        <tr>
          <td>Name</td>
          <td><input type="text" name="name" size="20">
          </td>
        </tr>
        <tr>
          <td>Salary</td>
          <td><input type="number" name="salary" size="40">
          </td>
        </tr>
        <tr>
          <td></td>
          <td align="right"><input type="submit" name="submit" value="Sent"></td>
        </tr>

        </form>
        </table>
      </td>
    </tr>
</table>
</body>
</html>
___________________________________________________________________________________


3. Create insert.php file
  
insert.php
___________________________________________________________________________________

<?php
   
    $link = mysqli_connect('localhost', 'root', '', 'test');
     if(isset($_POST["name"],$_POST["salary"] ))   
    {
        if(!empty($_POST["name"]) && !empty($_POST["salary"]) )
        {
            $name = $_POST["name"];
            $salary = $_POST["salary"];
            $add = "INSERT INTO employees(name,salary)VALUES('$name',$salary)";
            $result = mysqli_query($link, $add);
            if($result)
            {
                //echo("<br>Input data is succeed");
                header("location:search.php");
            }
            else
            {
                echo("<br>Input data is fail");
            }
        }
    }

?>
___________________________________________________________________________________


4. Create search.php file
    important lines shown in blue.
 
search.php
___________________________________________________________________________________

<html>
<head>
<title>Search data</title>
</head>
<body>
<table>
  <tr>
    <td align="center">EMPLOYEES DATA</td>
  </tr>
  <tr>
    <td>
      <table border="1">
      <tr>
        <td>ID</td>
        <td>EMPLOYEES<br>NAME</td>
        <td>SALARY</td>
      </tr>
    <?php
    $link = mysqli_connect('localhost', 'root', '', 'test');
    $search = "SELECT * FROM employees ORDER BY id";
    $result = mysqli_query($link, $search);
    while($data = mysqli_fetch_row($result))
    {
        echo("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr>");
    }
    echo("<br><br><tr><td><strong><a href=\"edit.php\">Edit</a></strong></td></tr>");
    ?>

    </table>
  </td>
</tr>
</table>
</body>
</html>
___________________________________________________________________________________


5. Create edit.php file
    important lines shown in blue.
 
edit.php
___________________________________________________________________________________
 <html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table>
  <tr>
    <td align="center">DATA</td>
  </tr>
  <tr>
    <td>
      <table border="1">
      <?php
      $link = mysqli_connect('localhost', 'root', '', 'test');

      $edit = "SELECT * FROM employees";
      $result = mysqli_query($link, $edit);
      while ($row = mysqli_fetch_array($result))
      {
        echo ("<tr><td>$row[id]</td>");
        echo ("<td>$row[name]</td>");
        echo ("<td>$row[salary]</td>");
        echo ("<td><strong><a href=\"edit_process.php?id=$row[id]\">Edit</a></strong></td>");
        echo ("<td><strong><a href=\"delete.php?id=$row[id]\">Delete</a></strong></td><br><br>");
        echo ("<td><strong><a href=\"input.php\">Add Employee</a></strong></td>");
      }
      ?>

      </table>
    </td>
   </tr>
</table>
</body>
</html>
___________________________________________________________________________________


6. Create edit_process.php file
    important lines shown in blue.
 
edit_process.php
___________________________________________________________________________________
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>
<table border=1>
  <tr>
    <td align=center>Form Edit Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
      <?php
      $link = mysqli_connect('localhost', 'root', '', 'test');
     
      $id1=$_GET['id'];
     
      $edit = "SELECT * FROM employees where id=$id1 ";
      $result = mysqli_query($link,$edit);
      $row = mysqli_fetch_array($result);
      ?>
      <form method="post" action="edit_btn.php">
      <input type="hidden" name="id" value=" <?php echo "$row[id]" ?> ">
        <tr>       
          <td>Name</td>
          <td>
            <input type="text" name="name" size="20" value="<?php echo "$row[name]"?>">
          </td>
        </tr>
        <tr>
          <td>Salary</td>
          <td>
            <input type="text" name="salary" size="40" value="<?php echo "$row[salary]"?>">
          </td>
        </tr>
        <tr>
          <td align="right">
            <input type="submit" name="submit value" value="Edit">
          </td>
        </tr>
      </form>

      </table>
    </td>
  </tr>
</table>
</body>
</html>
___________________________________________________________________________________


7. Create edit_btn.php file
 
edit_btn.php
___________________________________________________________________________________
<?php

$link = mysqli_connect('localhost', 'root', '', 'test');

$name=$_POST['name'];
$salary=$_POST['salary'];
$id=$_POST['id'];

$update = "UPDATE employees SET name='$name',salary=$salary WHERE id=$id";

mysqli_query($link,$update);

header("location:edit.php");

?>
___________________________________________________________________________________


8. Create delete.php file
 
delete.php
___________________________________________________________________________________
<?php
    $link = mysqli_connect('localhost', 'root', '', 'test');
   
    $id2=$_GET['id'];
    
    $delete = "DELETE FROM employees WHERE id=$id2 ";
    $result = mysqli_query($link,$delete);
    header("location:edit.php");
?>
___________________________________________________________________________________

Hope this helped....
Please leave a comment.
Suggestions for improvements are also welcomed :)

...Happy Coding...    

No comments:

Post a Comment