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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.