It edits the wrong row???

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

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

It edits the wrong row???

Post by cturner »

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();
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Your missing a WHERE clause...
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

everah i think we are tired lol
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Man I hate it when I miss the obvious... and astions doesn't. :wink:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

aye

hows it working now bud?
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

What do you mean by the WHERE clause astions? Can you please show me an example?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

[sql]UPDATE table SET field = data WHERE field = condition[/sql]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

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";
Post Reply