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.

 

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?

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;
}

?>

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.