liamloveslearning Posted May 17, 2010 Share Posted May 17, 2010 Hi all, im calling back data from a table which i need to then add an update and delete button to amend the record accordingly, Could anybody point me in the direction of how to add this function? Is it a big job? my current code is <table> <?php do { ?> <tr><td> <?php echo $row_usersawtappr['username']; ?></td><td><?php echo $row_usersawtappr['email']; ?></td><td><?php echo $row_usersawtappr['registration_date']; ?></td><td><?php echo $row_usersawtappr['dob']; ?></td><td>approve / delete</td></tr> <?php } while ($row_usersawtappr = mysql_fetch_assoc($usersawtappr)); ?> </table> Quote Link to comment Share on other sites More sharing options...
Stuie_b Posted May 17, 2010 Share Posted May 17, 2010 Try the following Assuming your table has an id field, the following creates a hyperlink to the file do.php passing the id value to do.php <table> <?php do { ?> <tr><td> <?php echo $row_usersawtappr['username']; ?></td><td><?php echo $row_usersawtappr['email']; ?></td><td><?php echo $row_usersawtappr['registration_date']; ?></td><td><?php echo $row_usersawtappr['dob']; ?></td><td><a href="do.php?act=approve&id=<?php echo $row_usersawtappr['id']; ?>">approve</a> / <a href="do.php?act=delete&id=<?php echo $row_usersawtappr['id']; ?>">delete</a></td></tr> <?php } while ($row_usersawtappr = mysql_fetch_assoc($usersawtappr)); ?> </table> do.php This file does the work, depending on the action to perform ($act), $act = $_GET['act']; $id = $_GET['id']; if($act =="approve"){ mysql_query("UPDATE %TABLENAMEHERE% set approve='1' WHERE id='".mysql_real_escape_string($id)."'"); header("location: index.php"); //Reloads the index page (change to the page used to list contents }elseif($act =="delete"){ mysql_query("DELETE FROM %TABLENAMEHERE% WHERE id='".mysql_real_escape_string($id)."'"); header("location: index.php"); //Reloads the index page (change to the page used to list contents } Replace %TABLENAMEHERE% with your tables name Stuie Quote Link to comment Share on other sites More sharing options...
beta0x64 Posted May 17, 2010 Share Posted May 17, 2010 Certainly, liamloveslearning! First of all, do you know how to update a value in MySQL? update TABLE set field="blah", field2="blahblah"; http://dev.mysql.com/doc/refman/5.5/en/update.html To delete a value, you can set it to NULL, but to delete a row or even table, you can use: http://dev.mysql.com/doc/refman/5.5/en/delete.html Second of all, I would personally use jQuery to add the buttons, then jQuery's AJAX functionality to call a PHP script where you can validate what the user is suggesting to put into the database. Once it's validated, react appropriately. Remember, you have to make sure "l33t h4x0rz" don't send any naughty code into your database... I recommend htmlentities or htmlspecialchars... There are a lot of well-documented ways to do this. Using jQuery also allows you to dynamically update the page instead of relying on redirects, which causes a jumpy and unpleasant user experience. It also reduces the load on the server itself (you don't have to call the script to list the database's contents again and again, only one record). This is not a purely PHP way to do it, as Stuie_b suggests, but it is certainly a good one, I think. Good luck! Quote Link to comment Share on other sites More sharing options...
liamloveslearning Posted May 17, 2010 Author Share Posted May 17, 2010 Got it working like a charm! thanks for being so descriptive and explaining everything Quote Link to comment 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.