Thursday, 2 May 2013

Delete record using php + MySQL

Delete Record Using PHP + MySQL.

If Any web programmer of Web Developer doing code for delete any record from the table then
1. He/She must have to see all the records of the table
2. And select the record to delete and press on delete button.
3. After deleting the record he/she must have to see the records of the table.

Here is the full code to see the records from the table and search the user according to the username.
If records are not more then see all the records by search with empty textbox and click on SUBMIT button.
After Search Result is generated Press on Delete.


Table Name of MySQL is tblreg
Have columns
1. id
2. username
3. hobbies
4. gender
5. emailid
6. mobileno

1. showreg.php file code


<?php
if(isset($_GET['submit']))
{
$s=trim($_GET['search']);
$con=mysqli_connect(@"localhost","root","","phpdemo") or die("Unable to connect to the database of Mysql name :  phpdemo");
if(empty($s))
{
$sql="select * from tblreg";
}
else
{
$sql="select * from tblreg where username='$s'";
}
echo "<center>";
echo "<h1 style=\"color:Yellow;background-color:Black \">Show Registration Details</h1>";
$result=mysqli_query($con,$sql);
echo "<table border=\"2\" style=\"color:Black;background-color:#ffffcc\">";
echo "<th>Userid</th><th>Username</th><th>Hobbies</th><th>Gender</th><th>Emailid</th><th>Mobileno</th>";
while($line= mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $line['id'];
echo "</td>";
echo "<td>";
echo $line['username'];
echo "</td>";
echo "<td>";
echo $line['hobbies'];
echo "</td>";
echo "<td>";
echo $line['gender'];
echo "</td>";
echo "<td>";
echo $line['emailid'];
echo "</td>";
echo "<td>";
echo $line['mobileno'];
?>
<input type="text" name="chid" value="<?php echo $line['id']; ?>" />
<?php
echo "</td>";

echo '<td><b><font color="#663300"><a href="delete.php?id=' . $line['id'] . '">Delete</a></font></b></td>';
echo "</tr>";

}
echo "</table>";

}
?>
<body>
<form id="showform" name="showform" method="get" action="">
<input type="text" name="search" />
<input type="submit" name="submit" value="submit" />
</form>



To Delete Record of The User write code on delete.php File.2.  delete.php After Deleting the record he/she must reach at showreg.php to see the records of the table tblreg.






<?php
$con=mysqli_connect(@"localhost","root","","phpdemo") or die("Unable to connect to the database of Mysql name :  phpdemo");
$id = $_REQUEST['id'];
echo "id is $id<br>";
$sql="DELETE FROM `tblreg` WHERE `id`='$id'";
$result = mysqli_query($con,$sql) or die("Error in Deleting!!!");
header("Location: showreg.php");
?>

No comments:

Post a Comment