Jump to content

Update/Delete Record


Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/202100-updatedelete-record/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/202100-updatedelete-record/#findComment-1059801
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/202100-updatedelete-record/#findComment-1059806
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.