view,edit and delete using PHP & Mysql

view,edit and delete using PHP & Mysql
view,edit and delete using PHP & Mysql



In this tutorial i will discuss how to do view,edit and delete using PHP & Mysql. In this code for viewing the data from database i.e Mysql i am using HTML table.First data will be configured into the HTML table from there data will be deleted and edited/Updated.


So lets check the tutorial on view,edit and delete using PHP & Mysql part by part.

So lets see the code for view from database ,save this php file as viewstudent.php

/*In this tutorial edit and delete script will be interlinked with the view page*/
<?php
$con=mysqli_connect("localhost","root","","youtube");
$query="select * from registration";
$sql=mysqli_query($con,$query);
?>
<title>View Example</title>
<p align="center">View Of Registration Details</p>
<hr/>
<table width="1264" height="150"  border="1" align="center">

  <thead>
  <th>Registration Id</th>
  <th>Station name</th>
  <th>fathers name</th>
  <th>Mothers  name</th>
  <th>Address</th>
  <th>Email</th>
  <th colspan="2">Action</th>
  </thead>
  <tbody>
  <?php
  while($rs1=mysqli_fetch_row($sql))
  {

    echo "<tr>";
    echo "<td align='center'>$rs1[0]</td>";
echo "<td align='center'>$rs1[1]</td>";
echo "<td align='center'>$rs1[2]</td>";
echo "<td align='center'>$rs1[3]</td>";
    echo "<td align='center'>$rs1[4]</td>";
    echo "<td align='center'>$rs1[5]</td>";
echo "<td align='center'><a href='delete.php?a=$rs1[0]'>Delete</a></td>";
echo "<td align='center'><a href='edit.php?a=$rs1[0]&b=$rs1[1]&c=$rs1[2]&d=$rs1[3]&e=$rs1[4]&f=$rs1[5]'>Edit</a></td>";
    echo "</tr>";
 
  }
?>
    </tbody>
  </table>

In the above code mysqli_fetch_row($sql),will fetch the result as row from the result set. After that the row which is fetched will be displayed in the table row.So this loop will run until there is data in the result set.

Now lets see the code for delete ,when the user will click the delete link in the above code delete.php will be called where the primary key is send via url.Save this php file as delete.php.


<?php
$con=mysqli_connect("localhost","root","","youtube");
$rid=$_GET["a"];
$sql=mysqli_query($con,"delete from registration where rid='$rid'");
if($sql==1)
{
header("Refresh:0;viewstudent.php");
}

?>

As the rid is send via url hence we need to collect the data using GET method.After the delete process is successful the user will be transferred to the view of student.

Now lets see the code of Update ,the user will get a form where the user have to update the data.The datas are send from view to update via url. So the query string is used.Save the file edit.php

<!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" />
<title>Untitled Document</title>
</head>
<?php
$rid=$_GET["a"];
$name=$_GET["b"];
$fname=$_GET["c"];
$mname=$_GET["d"];
$address=$_GET["e"];
$email=$_GET["f"];
?>

<body>
<form name="f1" method="post" action="editsc.php">
<table width="436" height="193" border="1" align="center">
  <tr>
    <td width="107">Registration Id</td>
    <td width="313"><input type="text" name="t1" id="textfield" value="<?php echo $rid;?>" readonly="readonly"/></td>
  </tr>
  <tr>
    <td>Name</td>
    <td><input type="text" name="t2" id="textfield2" value="<?php echo $name;?>"/></td>
  </tr>
  <tr>
    <td>Fathers Name</td>
    <td><input type="text" name="t3" id="textfield3" value="<?php echo $fname;?>"/></td>
  </tr>
  <tr>
    <td>Mothers Name</td>
    <td><input type="text" name="t4" id="textfield4" value="<?php echo $mname;?>"/></td>
  </tr>
   <tr>
    <td>Address</td>
    <td><textarea name="t5"><?php echo $address;?></textarea></td>
  </tr>
   <tr>
    <td>Email</td>
    <td><input type="text" name="t6" id="textfield4" value="<?php echo $email;?>"/></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="Update" /></td>
  </tr>
</table>
</form>
</body>
</html>

Here as you see if the update button is pressed than the datas will posted to another script page editsc.php

<?php
$con=mysqli_connect("localhost","root","","youtube");
$rid=$_POST["t1"];
$name=$_POST["t2"];
$fname=$_POST["t3"];
$mname=$_POST["t4"];
$address=$_POST["t5"];
$email=$_POST["t6"];

$a="update registration set name='$name',fathersname='$fname',mothersname='$mname',address='$address',email='$email' where rid='$rid'";
echo $a;
$sql=mysqli_query($con,$a);
if($sql==1)
{
header("Refresh:0;viewstudent.php");
}
?>

If you get any problem ,comment in the comment section.I will help out to solve the problem.enjoy coding.

The video tutorial for display data using php and mysql:


The Video Tutorial for How to delete from mysql  database using php :











Comments

Popular posts from this blog

Insert data into database using Android,php & Mysql

Random password Generator In PHP

How to select data from mysql database using PHP