Jump to content

Edit/update query results


Goon

Recommended Posts

When you retrieve data from a mysql table is it possible to have an edit option next to each of the returned results? 

 

I would like to have the option to update or delete results returned if possible.

Link to comment
Share on other sites

When you retrieve data from a mysql table is it possible to have an edit option next to each of the returned results? 

 

I would like to have the option to update or delete results returned if possible.

 

Yes, it's possible.  The most simple way to do it is to have an 'Edit/Delete' input button associated with each result.  How this association is formed depends on how the items you want to modify are structured. 

 

Once this button is clicked, it brings the user to an editing page which puts the data in form inputs that the user can modify.  Here there should be two more buttons: 'Save Modifications' and 'Delete Record'.  When 'Save Modifications' is clicked, the data in the form fields is updated in the DB.  When 'Delete Record' is pressed, the record is deleted from the DB.

 

There are other ways to get at the records...I've used checkboxes to specify multiple records to be edited/deleted.  It's not much harder to implement, once you get the basics down.

 

Can you work from here, or do you need to see some code?

Link to comment
Share on other sites

of course

 

<?php

if ( empty( $_GET["action"] ) ) {
$results = mysql_query( "SELECT * FROM table" );
while ( $result = mysql_fetch_object( $results ) ) {
	print $result->body . "<br />Edit Text:<br />
	<form action='this.php?action=edit&id=" . $result->id . "' method='post'>
	<textarea name='newbody' rows='10' cols='10'>" . $result->body . "</textarea><br />
	<input type='submit' value='Edit' /></form> <a href='this.php?action=del&id=" . $result->id . "'>Delete</a>";
}
die( );
}
switch ( $_GET["action"] ) {
case "edit":
	mysql_query( "UPDATE `table` SET `body`='" . mysql_real_escape_string( $_POST["newbody"] ) . "' WHERE `id`='" . settype( $_GET["id"], "int" ) . "'" );
	break;
case "del":
	mysql_query( "DELETE FROM `table` WHERE `id`='" . settype( $_GET["id"], "int" ) . "'" );
	break;
}

?>

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.