Add Edit Update Delete Using Php and Mysql
Last Updated on
Hi Guys, Every web programming language have database operations.
A very basic database operations are add,edit ,update and delete database records. In this tutorial I have to teach how to add ,edit,delete,update a record using php and mysql.
I have to add basic student details in database.I create and use four files to perform this basic database operations.
- Config.php
- Index.php
- edit-studentdetail.php
- Manage-students.php
Config.php
contains database connection Details.see the below code.
<?php $con=mysql_connect('localhost','root',''); //Database Connections $db=mysql_select_db('student',$con); // Database Name ?>
Index.php
index.php perform add student details in database .below code explain this operations.
<?php include('config.php'); // Database Config /* Add Student Detail Php */ if(isset($_POST['add'])) // Check form submit or not { $name=$_POST['name']; $gender=$_POST['gender']; $department=$_POST['dept']; $terms=$_POST['terms']; // Check all fields are empty or not if empty return error message if($name=='' && $gender=='' && $department=='') { // Assign Messge variable $msg="All fields are mandatary.Please fill It"; } else { $query="INSERT INTO `student_detail`(name,gender,department,terms)values('$name','$gender','$department','$terms')"; $result=mysql_query($query); if($result) { $msg="Student Detail Successfully Added"; // Success Message echo '<meta http-equiv="refresh" content="0;manage-studentdetail.php?msg='.$msg.'" />'; } } } ?> <h2><?php echo $msg ?></h2> <table align="center" width="400px" cellpadding="5" cellspacing="5" bgcolor="#E6E6E6" border="1" rules="all"> <tr> <td><a href="index.php">Add Student</a></td> <td><a href="manage-studentdetail.php">Manage Student</a></td> </tr> </table> <!-- Add Student Detail Form --> <h3 align="center">Add Student Detail</h3> <form method="post" action="<?php $_SERVER['PHP_SELF']?>"> <table align="center" width="500px" cellpadding="5" cellspacing="5"> <tr> <td>Name:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>Gender:</td> <td> <input type="radio" name="gender" value="Male" />Male <input type="radio" name="gender" value="Female" />Female </td> </tr> <tr> <td>Department:</td> <td> <select name="dept"> <option value="" selected="selected">Select Department</option> <option value="CSE">CSE</option> <option value="EEE">EEE</option> <option value="ECE">ECE</option> <option value="MECH">MECH</option> </select> </td> </tr> <tr> <td>Hobby:</td> <td> <input type="checkbox" name="terms" value="Yes"/> Accept Terms and conditions </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="add" value="Add student" /></td> </table> </form>
Edit-studentdetails.php
In this file perform edit and delete student record .see the below code
<?php include('config.php'); // Database Config /* Update Student Detail Php */ if(isset($_POST['uid'])) // Get id { $eid = $_POST['uid']; $name=$_POST['name']; $gender=$_POST['gender']; $department=$_POST['dept']; $terms=$_POST['terms']; if($name=='' && $gender=='' && $department=='') // Check all fields are empty or not if empty return error message { $msg="All fields are mandatary.Please fill It"; // Assign Messge variable } else { $query="UPdate `student_detail` SET name='$name',gender='$gender',department='$department',terms='$terms' WHERE id='$eid'"; $result=mysql_query($query); if($result) { $msg="Student Detail Updated"; // Success Message echo '<meta http-equiv="refresh" content="0;manage-studentdetail.php?msg='.$msg.'" />'; } } } ?> <?php $eid=$_GET['id']; $getdata=mysql_query("SELECT * FROM `student_detail` WHERE id='$eid'"); $row=mysql_fetch_array($getdata); ?> <h2><?php echo $msg ?></h2> <!-- Edit Student Detail Form --> <form method="post" action="<?php $_SERVER['PHP_SELF']?>"> <table align="center" width="500px" cellpadding="5" cellspacing="5"> <tr> <td>Name:</td> <td><input type="text" name="name" value="<?php echo $row['name']?>" /</td> </tr> <tr> <td>Gender:</td> <td> <input type="radio" name="gender" value="Male" <?php if($row['gender']=='Male') {?> checked="checked" <? } ?> />Male <input type="radio" name="gender" value="Female" <?php echo $row['gender']?> <?php if($row['gender']=='Female') {?> checked="checked" <? } ?> />Female </td> </tr> <tr> <td>Department:</td> <td> <select name="dept"> <option value="CSE" <?php if($row['department']=='CSE') {?> selected="selected" <? } ?>>CSE</option> <option value="EEE" <?php if($row['department']=='EEE') {?> selected="selected" <? } ?>>EEE</option> <option value="ECE" <?php if($row['department']=='ECE') {?> selected="selected" <? } ?>>ECE</option> <option value="MECH" <?php if($row['department']=='MECH') {?> selected="selected" <? } ?>>MECH</option> </select> </td> </tr> <tr> <td>Terms:</td> <td> <input type="checkbox" name="terms" value="Yes" <?php echo $row['terms']?><?php if($row['terms']=='Yes') {?> checked="checked" <? } ?>/>Accept Terms and conditions </td> </tr> <tr> <td colspan="2" align="center"> <input type="hidden" name="uid" value="<?php echo $eid ?>" /> <input type="submit" name="update" value="Update" /></td> </table> </form>
Next Manage-students.php
Manage-students.php perform show all students details fetch from database with edit and delete link. Edit link perform edit student record and update record operations, delete link perform delete a particular record in database.In the below code explain this operations
<?php include('config.php'); // Database Config /* Delete Student Detail Php */ if(isset($_GET['did'])) // Get Id { $did=$_GET['did']; $query="DELETE FROM `student_detail` WHERE id='$did'"; $result=mysql_query($query); if($result) { $msg="Student Detail Deleted Successfully "; // Success Message } } ?> <?php if(isset($_GET['msg'])) { ?> <h2 align="center"><?php echo $_GET['msg'] // Message From Edit Page?></h2> <? } ?> <h2 align="center"><?php echo $msg ?></h2> <table align="center" width="400px" cellpadding="5" cellspacing="5" bgcolor="#E6E6E6" border="1" rules="all"> <tr> <td><a href="index.php">Add Student</a></td> <td><a href="manage-studentdetail.php">Manage Student</a></td> </tr> </table> <!-- Manage Student Detail --> <table align="center" width="500px" cellpadding="5" cellspacing="5" border="1px" rules="all"> <tr> <td>S.no</td> <td>Name</td> <td>Gender</td> <td>Department</td> <td>Terms</td> <td>Edit</td> <td>Delete</td> </tr> <?php $i=1; $getdata=mysql_query("SELECT * FROM `student_detail`"); while($row=mysql_fetch_array($getdata)) { ?> <tr> <td><?php echo $i ?></td> <td><?php echo $row['name'];?></td> <td> <?php echo $row['gender'];?> </td> <td><?php echo $row['department'];?></td> <td> <?php echo $row['terms'];?> </td> <td><a href="edit-studentdetail.php?id=<?php echo $row['id']?>">Edit</a></td> <td><a href="manage-studentdetail.php?did=<?php echo $row['id']?>">Delete</a></td> </tr> <? $i++; } ?> </table>
I hope in this tutorial give a basic idea about database operations using php and mysql. If you any doubt please leave a comment.
I’m totally new in php
and i just want to know that how this is actually work
(
if(isset($_POST[‘uid’]))
)
can you please tell how this statement is actaully work.
and why should we
$query=”UPdate `student_detail` SET name=’$name’,gender=’$gender’,department=’$department’,terms=’$terms'”;
$result=mysql_query($query);
again pass the values to $result variable if we already pass it in the $query variable?
I know this very simple things but until i don’t know why I’m writing that things I’ll never become a master in php pls help me
thanks for great help..
1 small issue is that when i update 1 row of data , all other data gets set to same data..