Create dynamic sites with PHP & MySQL - Deleting rows
(Page 13 of 16 )
So far we have only entered new information in our database and viewed it. Where's the fun if we can't trash some of those data, at least the useless ones? Our delete.php will do just that. It works exactly like view.php. The only difference is the SQL command "DELETE FROM personnel WHERE id=$id", which tell MySQL to delete the row that contains the id corresponding to the variable $id. Generally, the SQL command for deleting a row is DELETE FROM database_name WHERE field_name=somevalue
<HTML> <?php $db = mysql_connect("localhost", "root", ""); mysql_select_db("learndb",$db); mysql_query("DELETE FROM personnel WHERE id=$id",$db); echo "Information Deleted"; ?> </HTML> |
Once again we modify our previous viewdb2.php script to viewdb3.php to add this new feature. The additions should be obvious.
<HTML> <?php $db = mysql_connect("localhost", "root", ""); mysql_select_db("learndb",$db); $result = mysql_query("SELECT * FROM personnel",$db); echo "<TABLE BORDER=2>"; echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Options</B></TR>"; while ($myrow = mysql_fetch_array($result)) { echo "<TR><TD>".$myrow["firstname"]." ".$myrow["nick"]; echo "<TD><a href=\"view.php?id=".$myrow[id]."\">View</a> "; echo "<a href=\"delete.php?id=".$myrow[id]."\">Delete</a>"; } echo "</TABLE>"; ?> </HTML> |
Try clicking on delete and then view the database again with viewdb3.php to verify that the row was really deleted. You may have to refresh your browser.
Next: Editing data >>
More Database Articles Articles
More By Codewalkers
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|