Delete Records from multiple tables php mysql
Add a delete link to the display file as bellow.By pressing delete link delete records from 3 tables.
| |||||||||||||||
<?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_database",$con); $query='SELECT divition.uid,divition.uname, divition.division, institute.institute, job_title.job_title FROM divition,institute,job_title WHERE divition.uid = institute.iid AND divition.uid = job_title.tid' ; $result = mysql_query($query); ?> <table bgcolor=#000000 align="center"> <tr> <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Division</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Institute</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Job Title</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Delete</strong></td> </tr> <?php while ($rows = mysql_fetch_assoc($result)){ ?> <tr> <td bgcolor="#FFFFFF"><?php echo $rows['uname']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['division']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['institute']; ?></td> <td bgcolor="#FFFFFF"><?php echo $rows['job_title']; ?></td> <td bgcolor="#FFFFFF"><a href="delete.php?id=<?php echo $rows['uid']; ?>">delete</a></td> </tr> <?php } mysql_close($con); ?> |
delete.php
<?php $host="localhost"; $username="root"; $password=""; $db_name="my_database"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $id=$_GET['id']; $sql="DELETE FROM divition WHERE divition.uid ='$id'"; $result=mysql_query($sql); if($result) { echo "Deleted from divition Successfully</br>"; } else { echo "ERROR"; } $sql1="DELETE FROM institute WHERE institute.iid ='$id'"; $result1=mysql_query($sql1); if($result1) { echo "Deleted from institute Successfully</br>"; } else { echo "ERROR"; } $sql1="DELETE FROM job_title WHERE job_title.tid ='$id'"; $result1=mysql_query($sql1); if($result1) { echo "Deleted from job_title Successfully"; } else { echo "ERROR"; } mysql_close(); ?> |
No comments:
Post a Comment