jagguy Posted November 5, 2006 Share Posted November 5, 2006 Hi,If I want to edit/delete a mysql record with php, how can i do that. It is easy to display records and add new records , but editing them means you need to display records and somehow indicvate a record needs to be changed. You can't simply do this on a table it looks like.I am after the general idea and not exact code. Link to comment https://forums.phpfreaks.com/topic/26200-edit-with-php/ Share on other sites More sharing options...
Skatecrazy1 Posted November 5, 2006 Share Posted November 5, 2006 okay first of allget the row value into an arrayecho what you need into a textareathen in the action file of that form, say something likeUPDATE `table` SET `field1`='$field_new_text', `field2`='$otherfield_new_text'to delete it, list all of your row data out (assuming you know how to do this already) and have a link similar to this: delete.php?id=$row['id']then, on the next file (delete.php), create a get variable, $id = $_GET['id'];the SQL should look likeDELETE FROM `table` WHERE `id`="$id" LIMIT 1and that should get you started.if you need code examples say so Link to comment https://forums.phpfreaks.com/topic/26200-edit-with-php/#findComment-119825 Share on other sites More sharing options...
jagguy Posted November 5, 2006 Author Share Posted November 5, 2006 hi,I can display and use the update,delete commands with mysql. It is coding the idea with a form is tricky.For the update/delete form, could you have a checkbox beside each row and post the checked boxes to a php form. Making sure each check box had a name you could test to see whether its value is true or false , and then match up the desired row on another webpage with the desired rows to change.This would again have a link to another php webpage to make the desired changes. Link to comment https://forums.phpfreaks.com/topic/26200-edit-with-php/#findComment-119862 Share on other sites More sharing options...
Skatecrazy1 Posted November 6, 2006 Share Posted November 6, 2006 Well, I sort of get what you're saying, but not really; so I'll show you how I do it sometimes.this is the file that lists the rows with links to edit/delete them[code]<?php//connect and stuff$conn = @mysql_connect("server", "user", "pass") or die(mysql_error());$rs = @mysql_select_db("database") or die(mysql_error());//write sql$sql = "SELECT * FROM `table` ORDER BY `id` DESC";//execute the sql query$rs = @mysql_query($sql, $conn) or die(mysql_error());//prepare table to echo data$msg = "<table cellspacing=\"0\" cellpadding=\"3\" border=\"1\">";$msg .= "<h3>Row Information</h3>";//loop thru the array to echo the data in table rowswhile($row = mysql_fetch_array($rs)){$msg .= "<tr><td>".$row['field_name']."</td>";$msg .= "<td><a href=\"delete.php?id=".$row['id']."\">Delete Row</a>";$msg .= " <a href=\"edit.php?id=".$row['id']."\">Edit Row</a>";$msg .= "</td></tr>";}$msg .= "</table>";echo $msg;?>[/code]then, as far as deleting a row, follow this kind of layout for delete.php; remember we passed a get value with the id of the row we want to delete by clicking on the link generated by our loop[code]<?php//delete.php//this is important; this is the GET value passed from the link which will//tell us which row to delete in the SQL$id = $_GET['id'];$conn = @mysql_connect("server", "user", "pass") or die(mysql_error());$rs = @mysql_select_db("database") or die(mysql_error());//write deleting query$sql = "DELETE FROM `table` WHERE `id`=\"$id\" LIMIT 1;//execute query@mysql_query($sql, $conn) or die(mysql_error());?>[/code]come back l8r for the edit code; i have to go pick someone up @ the airport Link to comment https://forums.phpfreaks.com/topic/26200-edit-with-php/#findComment-120168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.