Friday, 16 September 2016

How to Edit & Delete table records Dynamically in PHP & mysql:Dynamically Edit & Delete table records in PHP

This code Describes how to Edit &  Delete records dynamically. we already know how to update and delete records in table manually using sql query but it tells you how to make those changes dynamically from your web application.for doing this you have to create table with the name of "employee", then create columns id,emp_name,designation,city.

here there are three pages:

1) view.php.
2) edit.php.
3) delete.php.

In the view.php is just a html page we are viewing a table content from  database and creating links for edit and delete using id.this id passed to the edit.php and delete.php page.

editing table records first display the table datas,then create new page to delete and edit.

here first view page:

view.php
*************
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    <?php
        $con=mysql_connect('localhost','root','') or die("could not        connect");
        mysql_select_db('mini_project') or die("database could not      connected");
        $sql="select id,emp_name,designation,city from employee";
       $result=mysql_query($sql);
       if(! $result) 

           {
              die('Could not get data: ' . mysql_error());
           }
   ?>
<table width="600" border="1" cellpadding="1" cellspacing="1">
        <tR>
             <TD width="30%">NAME</TD>
             <TD width="40%">DESIGNATION</TD>
             <TD width="30%">SALARY</TD>
             <TD width="30%">action</TD>
       </tr>
<?php
         $i=1;
         while($rows=mysql_fetch_array($result))
           {
                 if($i%2==0)
           {
               echo '<tr bgcolor="#CCCCCC">';
            }
        else
          {
                 echo '<tr bgcolor="#FFFFFF">';
          }
             $i++;
?>


    <TD width="30%"><?php echo $rows['emp_name']; ?></TD>
    <TD width="40%"><?php echo $rows['designation']; ?></TD>
    <TD width="30%"><?php echo $rows['city']; ?></TD>





    <?php
             echo "<td><a href='edit.php?edit=".$rows['id']."'>Edit</a></td>";
             echo "<td><a href='delete.php?del=".$rows['id']."'>delete</a></td>";
    ?>/*here links to edit and delete php page with reference of id*/
</tr>
<?php
}
?>
</table>
<?php
mysql_close($con);

?>

************
After the view.php create another php file to edit the data.Here we just get a id number from view.php page and selecting records to those id. then updating as our changes

then write programme to edit in edit.php page

******************
edit.php
_____
 <?php
$con=mysql_connect('localhost','root','') or die("could not connect");
mysql_select_db('mini_project') or die("database could not connected");
if(isset($_GET['edit']))
{
$id=$_GET['edit'];
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$desi=$_POST['desi'];
$city=$_POST['city'];

$query3=mysql_query("update employee set emp_name='$name', designation='$desi', city='$city' where id='$id'");
if($query3)
{
header('location:view.php');
}
}

$query1=mysql_query("select * from employee where id='$id'");
$row=mysql_fetch_array($query1);

?>
<form action=""  method="post">
NAME<input type="text" name="name" id="name" value="<?php echo $row['emp_name']; ?>"></br>
DESIGNATION<input type="text" name="desi" id="desi" value="<?php echo $row['designation']; ?>"></BR>
CITY<input type="text" name="city" id="city" value="<?php echo $row['city']; ?>"></BR>
<input type="submit" name="submit" value="update">
</form>
<?php
}
?>

*********************************

After that create delete.php page this page simply execute delete comment as the id received from view,php page


Then write code to delete in delete.php.here codes:

delete.php:
_______

<?php
if(isset($_GET['del']))
{
$con=mysql_connect('localhost','root','') or die("could not connect");
mysql_select_db('mini_project') or die("database could not connected");
$id=$_GET['del'];
$sql="delete from employee where id='$id'";

echo $sql;
$result=mysql_query($sql) ;
if(! $result) {
      die('Could not get data: ' . mysql_error());
     
 }
 else
 {
header('Location:view.php');
 }

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

<body>
</body>
</html>

No comments:

Post a Comment