PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
cturner
Forum Contributor
Posts: 153 Joined: Sun Jul 16, 2006 3:03 am
Location: My computer
Post
by cturner » Tue Jul 18, 2006 1:49 am
Why is the following code editing the top row and not the selected row?
Code: Select all
require "config.php";
$query = 'UPDATE diary_contents SET diary_entry = \''.$_POST['diary_entry'].'\' LIMIT 1';
if (mysql_query ($query)) {
print '<p>The diary entry has been modified. <a href=view.php>Click here</a> to continue.</p>';
} else {
print "<p>Could not add the entry because: <b>" . mysql_error() .
"</b>. The query was $query.</p>";
}
mysql_close();
daedalus__
DevNet Resident
Posts: 1925 Joined: Thu Feb 09, 2006 4:52 pm
Post
by daedalus__ » Tue Jul 18, 2006 1:51 am
It's hard to say but you should be checking the ID of the diary entry to make sure it is editing the right one, not the content of the entry.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Jul 18, 2006 1:51 am
Before running the query, echo out the form data...
Code: Select all
require "config.php";
echo $_POST['diary_entry'] . ' is what was posted<br /><br />';
$query = 'UPDATE diary_contents SET diary_entry = \''.$_POST['diary_entry'].'\' LIMIT 1';
if (mysql_query ($query)) {
print '<p>The diary entry has been modified. <a href=view.php>Click here</a> to continue.</p>';
} else {
print "<p>Could not add the entry because: <b>" . mysql_error() .
"</b>. The query was $query.</p>";
}
mysql_close();
Last edited by
RobertGonzalez on Tue Jul 18, 2006 1:51 am, edited 1 time in total.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Jul 18, 2006 1:51 am
Your missing a WHERE clause...
daedalus__
DevNet Resident
Posts: 1925 Joined: Thu Feb 09, 2006 4:52 pm
Post
by daedalus__ » Tue Jul 18, 2006 1:52 am
everah i think we are tired lol
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Jul 18, 2006 1:52 am
Man I hate it when I miss the obvious...
and astions doesn't .
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Jul 18, 2006 1:53 am
Daedalus- wrote: everah i think we are tired lol
Yeah, I need to be up soon, so I should get off the boards. It is just so stinking hot right now and my house is sweltering. Anyways, enough thread killing.
Good catch astions.
daedalus__
DevNet Resident
Posts: 1925 Joined: Thu Feb 09, 2006 4:52 pm
Post
by daedalus__ » Tue Jul 18, 2006 1:54 am
aye
hows it working now bud?
cturner
Forum Contributor
Posts: 153 Joined: Sun Jul 16, 2006 3:03 am
Location: My computer
Post
by cturner » Tue Jul 18, 2006 2:59 am
What do you mean by the WHERE clause astions? Can you please show me an example?
daedalus__
DevNet Resident
Posts: 1925 Joined: Thu Feb 09, 2006 4:52 pm
Post
by daedalus__ » Tue Jul 18, 2006 3:01 am
[sql]UPDATE table SET field = data WHERE field = condition[/sql]
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Jul 18, 2006 3:02 am
If you don't tell MySQL what record to update it will update ALL of them. Yours isn't because you have LIMIT set.
Code: Select all
$query = "UPDATE `diary_contents` SET `diary_entry`='" . mysql_real_escape_string($_POST['diary_entry']) . "' WHERE `field`='" . $thisRecordNumber . "' LIMIT 1";