Page 1 of 1

Php script to edit tables entries

Posted: Mon Dec 18, 2006 1:33 am
by davidppppppppppp
My problem really needs to be addressed by someone who has the book "Php, MySQL and Apache All in One 2nd edition" by Julie C. Meloni and is fluent in Php.

I did the "Creating an Online Address Book" tutorial and I have it up and running - no problems. But If you're familiar with this particular tutorial you know that it lacks an essential script.

With this tutorial you can:
** Add someone to your address book.
** View the information of someone in your address book.
** Add to the information of someone already in your address book.

But strangely, you cannot Edit an entry in your address book.

I have worked on an editentry.php script to edit an entry in my address book until my fingertips are aching, but I just can't get it.

It would be great if someone else here has also noticed that this particular address book needs an Edit script and HAS one that I could look at!

Thanks,

David P.

Posted: Mon Dec 18, 2006 9:09 am
by iknownothing
I havent read the book, but I'll try to help anyway.

How is your (edit page) address book layed out? with tables, or have you used text boxes? It may be only possible with Text boxes, but if not, is much easier anyway.

Below is 2 files, the first one displays ALL names in your address book, and their addresses and phone numbers etc (when you edit the script a little further based on your database field names). You can edit the test within each as they are within text boxes. Upon clicking the update button it will go through the database update process, changing the field values to which you have editted.

ID as their to identify which entry to edit. If you do not currently have an id field in your database you will need to add one if you use this code.

FIRST FILE:

Code: Select all

$showname = MYSQL_QUERY("SELECT * FROM table_name");

 while ($row = mysql_fetch_assoc($showname))
  {

$name = $row['name'];
$id = $row['id'];

?>

<form type="POST" action="second_file_name">
<input type="hidden" value="<? echo $id; ?>" name="id">
<input type="text" value="<? echo $name; ?>" name="name">
<input type="sumbit" value="UPDATE">

<?

}

SECOND FILE:

Code: Select all

$name = $_POST['name'];
$id = $_POST['id'];

$sql = mysql_query("UPDATE tablename SET name = '$name' WHERE id ='$id'", $con);
		 		if ($sql) {	// mysql saved ok
						$save_result = "<b>Updated</b>";
				} ELSE	{	// mysql failed to save
						$save_result = "<font color=red><b>Failed</b></font>";
				}
I've used $name as an example, I would assume you'll need to add in such things as address, phone, etc.