Page 1 of 1

It edits the wrong row???

Posted: Tue Jul 18, 2006 1:49 am
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();

Posted: Tue Jul 18, 2006 1:51 am
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.

Posted: Tue Jul 18, 2006 1:51 am
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();

Posted: Tue Jul 18, 2006 1:51 am
by Benjamin
Your missing a WHERE clause...

Posted: Tue Jul 18, 2006 1:52 am
by daedalus__
everah i think we are tired lol

Posted: Tue Jul 18, 2006 1:52 am
by RobertGonzalez
Man I hate it when I miss the obvious... and astions doesn't. :wink:

Posted: Tue Jul 18, 2006 1:53 am
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.

Posted: Tue Jul 18, 2006 1:54 am
by daedalus__
aye

hows it working now bud?

Posted: Tue Jul 18, 2006 2:59 am
by cturner
What do you mean by the WHERE clause astions? Can you please show me an example?

Posted: Tue Jul 18, 2006 3:01 am
by daedalus__
[sql]UPDATE table SET field = data WHERE field = condition[/sql]

Posted: Tue Jul 18, 2006 3:02 am
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";