Jump to content

[SOLVED] mysql edit update


justAnoob

Recommended Posts

Items should have a unique id for themselves (such as an auto incrementing primary key) so that you can edit other information (such as item name) without changing the item's uniqueness.  That would solve some problems.  Otherwise you will have to do it your way, changing the name at the beginning , but looking for the old name ($gotit) to search on.. (bad practice).

  • Replies 55
  • Created
  • Last Reply

Okay, before you had your WHERE clause set to "WHERE id='$something'", your WHERE clause went something like "WHERE item_name='$gotit'" right?

 

Well, say you have these item_names in the DB:

one

two

three

 

Then you update one to four. Problem now becomes you're using WHERE to check for where item_name is four and you don't have one. Maybe if you used the OLD item_name, it would work. But ID is the way to go because you should never change an ID field.

the only problem with that is that I can't use the user_id to find the info in the database,, only because a user may have more than item in the database,,,

 

so wouldn't it be better to find the actual id of that specific item by saying something like

select id where item_name =$item_name, description =$description  etc...

You don't see the problem with this set up? You have *multiple* input fields with the same name generated via the while loop. So if the user has 3 entries, the while loop runs 3 times and there will be 3 duplicate entries of each name field. So if you just select them using $_POST, you only get the last one.

while ($row = mysql_fetch_array($sql))
{
echo "<tr><td width='188' height='180'><div align='center'>";
echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath2'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath3'] . '" width="125" alt="" />';
echo "</div></td><td width='188'><div align='center'>";
echo '<img src="' . $row['imgpath4'] . '" width="125" alt="" />';
echo "</div></td><td width='190'><div align='center'>";
echo '<img src="' . $row['imgpath5'] . '" width="125" alt="" />';
echo "</div></td></tr><tr><td height='43' colspan='4'>";
echo '<strong>Item Name:</strong> <input type="text" name="item_name" value="' . $row['item_name'] . '" size="50" />';
echo "</td><td><div align='center'>";
echo '<input type="submit" name="submit" id="submit" value="Update Trade">';
echo "</div></td></tr><tr><td height='116' colspan='4'>";
echo '<strong>Description:</strong> <textarea name="description" cols="75" rows="5">' . $row['description'] . '</textarea>';
echo "</td><td><div align='center'>";
echo "</div></td></tr><tr><td height='124' colspan='4'>";
echo '<strong>Seeking:</strong>       <textarea name="in_return" cols="75" rows="5">' . $row['in_return'] . '            </textarea>';
echo "</td><td><div align='center'>";
echo "</div></td></tr><tr><td colspan='4'>";
//echo "save delete buttons";
echo "</td><td><div align='center'>";
echo "</td></tr>";
}

 

ALL of that is in a while loop. Say there are 3 entries. That means each one of those echo statement will be ran trice. Following so far? Well if they are, then you would see that all the fields have the same name attribute - in_return, description, submit, etc. If you then submit the form and use $_POST['description'], you will *only* get the description of the *last* textarea instead of the first and or second. So you missed out on those data.

But only 1 item will be in front of the user at a time to be able to update,,, and if you are updating an item by selecting the items id,, it should only edit that specific item.......yes there is a while loop there and all the items will be displayed on the page,,, but I'm taking this one step at a time,,,after we can get this to work,,, I will setup a next and previous button so only 1 item is viewable to edit at a time.

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.