derekbelcher Posted April 10, 2009 Share Posted April 10, 2009 I am trying to build a simple content management system and am not real sure where to go. I have the code (below) that displays my table contents. Works great! What I want to do is make that "delete" word clickable and execute a delete function of that row's data. <?php $result = mysql_query("SELECT * FROM admin"); echo "<table border='1'> <tr> <th>First Name</th> <th>Last Name</th> <th>Email Address</th> <th>DELETE USER</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['firstname'] . "</td>"; echo "<td>" . $row['lastname'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td align='center'>" . delete; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/153531-solved-editing-my-database-from-the-web/ Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 echo '<td align="center"><a href="delete.php?id='.$row['id'].'">delete</a>'; That's presuming your primary key is called "id" Then inside delete.php use $_GET['id'] to get the parameter you're passing it. Quote Link to comment https://forums.phpfreaks.com/topic/153531-solved-editing-my-database-from-the-web/#findComment-806716 Share on other sites More sharing options...
AdRock Posted April 10, 2009 Share Posted April 10, 2009 echo '<td align="center"><a href="delete.php?id='.$row['id'].'">delete</a>'; That's presuming your primary key is called "id" Then inside delete.php use $_GET['id'] to get the parameter you're passing it. Exactly what I was going to say but he beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/153531-solved-editing-my-database-from-the-web/#findComment-806722 Share on other sites More sharing options...
derekbelcher Posted April 10, 2009 Author Share Posted April 10, 2009 alright. That's awesome and I think right where I need to be. Now, here comes my "newbieness" what should my delete.php have in it. I am just starting to learn some of the different functions, but haven't used the GET one yet. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/153531-solved-editing-my-database-from-the-web/#findComment-806730 Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 $_GET will get values from the URL $_POST gets data from a form with method of post $_REQUEST will get from loads of sources including the above two and cookies Once you have your ID retrieved from $_GET the next step depends if you want to verify with the user that it's the right record to delete. Either way later on you'll need to pass it to a DELETE query like this: $rowid=intval($_GET['id']); if (mysql_query("DELETE FROM admin WHERE id=".$rowid." LIMIT 1")) { echo 'Deleted'; } else { echo 'Not deleted'; } We add "LIMIT 1" as a precaution to make sure we only delete 1 record. We also use intval() on $_GET because we're expecting a number. Anything other than a number will return 0. Quote Link to comment https://forums.phpfreaks.com/topic/153531-solved-editing-my-database-from-the-web/#findComment-806737 Share on other sites More sharing options...
derekbelcher Posted April 10, 2009 Author Share Posted April 10, 2009 That worked out just perfect! You mentioned something about displaying a message asking the user if they were sure they wanted to delete that record. How hard is it to do that. Quote Link to comment https://forums.phpfreaks.com/topic/153531-solved-editing-my-database-from-the-web/#findComment-806774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.