PDA

View Full Version : Editting a database value


masterofollies
07-03-2008, 04:03 AM
I am trying to figure out how to edit a field in a database when a user wants to change something. So they make new information, submit it, the database inserts a new query. Now I need it where there is a choice to edit it. Here is the coding I am using to show all the information they have entered.

$check = mysql_query("SELECT date,location,info FROM reliclog WHERE `user` = '{$_SESSION['user_id']}'") or die(mysql_error());



echo 'This is the history log area.<br><br><br>';
echo '<table width="95%" border="1">';
echo '<tr><td width="10%"><b>Date</b></td><td width="25%"><b>Location</b></td><td width="60%"><b>Relic</b></td></tr>';
while ($row = mysql_fetch_array($check)) {
$date = $row["date"];
$info = $row["info"];
$location = $row["location"];


echo "<tr><td width=\"10%\">$date</td><td width=\"25%\">$location</td><td width=\"60%\">$info</td></tr>";
}
echo '</table>';

Can anyone point me in the right direction how to add a Edit tag that picks the exact piece of information they want to change? There is 2 things in information,
1. id - it adds +1 everytime something is inserted into the database.
2. user - this is the id of the user that uploaded the new information.

fl00d
07-03-2008, 05:35 AM
Hmm you could set up your table as a form too and add a checkbox called edit with a value of the id to each table row and then have an edit button. Then, the user simply checks the boxes of the rows they want to edit.

To edit info in MySQL, you would want to use the UPDATE clause.
ex:

$editSql = "UPDATE `reliclog` SET `info`='$newInfo' WHERE `user`='{$_SESSION['user_id']}' LIMIT 1";
$editQuery = mysql_query($editSql) or die(mysql_error());


Also, I noticed a problem with your $check SQL - you forgot to select the 'user' row.
"SELECT date,location,info FROM reliclog WHERE `user` = '{$_SESSION['user_id']}'"
should be

"SELECT `user`,`date`,`location`,`info` FROM `reliclog` WHERE `user` = '{$_SESSION['user_id']}'"

MySQL Update Syntax (http://dev.mysql.com/doc/refman/5.0/en/update.html)

masterofollies
07-03-2008, 02:25 PM
Hmm alright, and would it be easier to just have a Edit all button, where it lists all information at once? I wanted single but don't know if its possible. Thanks for seeing how I missed user.

ohgod
07-03-2008, 06:39 PM
if you use ajax you could do them one at a time

onFocus could make the field editable, onBlur could submit it and have it run in the background then update. just an idea.

masterofollies
07-04-2008, 01:04 AM
I don't know the smallest thing about Ajax at all