Forum Moderators: coopster

Message Too Old, No Replies

Need some help with Update function

replacing a single field in a MySQL db

         

mcjohnson

12:27 am on Aug 21, 2007 (gmt 0)

10+ Year Member



Hi friends,

What I would like to do is have a simple form where the user can log on and update a list of neighborhoods.

The DB has a table called "neighborhoods".

I have the form set up to display the data from the table within a text area, so taht the user can make any changes, click "update" and it sends the new data back to the table.

Problem is, I've fooled around with insert, and it is creating new rows and thus duplicating the data. I've played with UPDATE but can't seem to get it to work. If anyone can give me a hint as to where I am going wrong, I would appreciate it. Here's the code:

$query = "update neighborhoods
SET
('".$_POST['neighborhoods']."')";
$result = mysql_query($query)
or die ("Unable to Add item. Please contact administrator");

The form is obviously posting one field, "neighborhoods". If I use "Insert Into..." in the query, it adds another row. Essentially all I want to happen is for the new $_POST to replace the existing data in the table.

ANy help would be greatly appreciated.

Pat

Habtom

5:03 am on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you pull out the list of the neighborhoods, you should be able to pull out the id which makes a certian row unqiue. I assume you have your table structure with an incremental id within it.

While updating, you should be able to loop and run the update query as many list of neighborhoods you have, each updating its own row.

while (....) {
$query = "update neighborhoods SET ('".$_POST['neighborhoods']."') WHERE id = '". $_POST['id'] ."'";
}

Habtom