Jump to content

edit with php


jagguy

Recommended Posts

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

okay first of all

get the row value  into an array
echo what you need into a textarea
then in the action file of that form, say something like
UPDATE `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 like

DELETE FROM `table` WHERE `id`="$id" LIMIT 1

and 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

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

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 rows
while($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

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.