May 14, 200917 yr sorted my script from earlier :-D im wanting to add a delete & edit button in a table with the data from a mysql table i know the sql syntax but wondering how to incorporate would it be best to run to a new page or a function within the current page? e.g. the sql codes would be <b>edit</b> mysql_query("UPDATE xx SET name= '.$newname.' WHERE ID = '$id' "); <b>delete</b> mysql_query("DELETE FROM xx WHERE ID='ID'"); now should i create a php function e.g function edit & function delete or have a link based idea
May 15, 200917 yr sorted my script from earlier :-D im wanting to add a delete & edit button in a table with the data from a mysql table i know the sql syntax but wondering how to incorporate would it be best to run to a new page or a function within the current page? e.g. the sql codes would be <b>edit</b> mysql_query("UPDATE xx SET name= '.$newname.' WHERE ID = '$id' "); <b>delete</b> mysql_query("DELETE FROM xx WHERE ID='ID'"); now should i create a php function e.g function edit & function delete or have a link based idea How about a CMD eg http://localhost/act.php?cmd=delete
May 15, 200917 yr The way I would do this is to have the delete button link you to the same page, but pass some data to the page (using GET or POST) lets say your current page is http://www.atable.com/table.php, you could have the button link to http://www.atable.com/table.php?action=delete,id=5 You can then check with php for an action when the page loads, and then use switch/case for the possible values. if isset($_GET['action']){ switch($_GET['action']){ case "delete": $query = "DELETE FROM xx WHERE ID = '$_GET[id]'"; break; case "edit": $query = "UPDATE xx SET name= '.$_GET["newname"].' WHERE ID = '$_GET[id]' " break; } mysql_query($query,$conn); //obviously you'd have to have a connection object set up as $conn for this to work. } Alternatively you could do the same thing using $_POST variables, but you'd have to have some javascript that changed the values of some hidden fields on the form before, so get is a bit simpler (but easier for the user to edit, and cause some big screw-up!). EDIT: if you'd rather do it the POST way, but don't know how, then let me know.
Create an account or sign in to comment