Forum Moderators: coopster

Message Too Old, No Replies

trouble updating a mysql row

         

generic

10:35 pm on Apr 20, 2008 (gmt 0)

10+ Year Member



Hi,

I'm having trouble updating a record in mysql. I've tried a ton of variations and I can't seem to find my problem (I'm pretty bad with PHP lol)

Anyway, here's the code I'm using. If anyone can spot the problem, it'd be much appreciated.

<?php
// grab news id from url
$n = $_REQUEST['n'];

// grab form variables
$news_title = $_POST['news_title'];
$news_article = $_POST['news_article'];

$dbtable = 'news';

if($_REQUEST['submitted']==1) { // if information was posted, insert the data into the db
$result = mysql_query("UPDATE $dbtable SET news_title='$news_title', news_article='$news_article' WHERE news_id='$n'") or die(mysql_error());

// uncomment below for troubleshooting
//printf("Records updated: %d\n", mysql_affected_rows());

echo "<p>News entry saved. Click <a href=\"test.php\">here</a> to see the results.</p>";

} else { // if no information was posted, show the FCKeditor

$result = mysql_query("SELECT * FROM $dbtable WHERE news_id = '$n'") or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$news_id = $row["news_id"];
$news_title = $row["news_title"];
$news_article = $row["news_article"];
$news_date = $row["news_date"];
$news_expire = $row["news_expire"];

echo "<h1>Update News Article</h1>
<p>Please use the control below to edit existing articles in the database. It works like any word processor (like Microsoft Word or Works) so it should be pretty straightforward!</p>
<hr />
<form action=\"".$PHP_SELF."\" method=\"post\">
<p>News Title: <input type=\"text\" name=\"news_title\" value=\"".$news_title."\"></p>";

// initialise FCKeditor
$oFCKeditor = new FCKeditor('news_article') ;
$oFCKeditor->BasePath = 'fckeditor/' ;
$oFCKeditor->ToolbarSet = 'Basic';
$oFCKeditor->Value = $news_article ;
$oFCKeditor->Create() ;

echo "<input type=\"submit\" value=\"submit\">
<input type=\"hidden\" name=\"submitted\" value=\"1\">
</form>";
} // end of while
}
?>

IanKelley

12:18 am on Apr 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unless your FCKeditor class is doing it, you're not POSTing the news_id to the update script.

generic

1:32 am on Apr 21, 2008 (gmt 0)

10+ Year Member



No FCKeditor isn't posting anything like that. I just tried:


$result = mysql_query("UPDATE $dbtable SET news_title='{$_POST['news_title']}', news_article='{$_POST['news_article']}' WHERE news_id='{$_POST['n']}'") or die(mysql_error());

With no luck. Was that what you meant?

IanKelley

2:57 am on Apr 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here is the HTML for your form:

<form action=\"".$PHP_SELF."\" method=\"post\"> 
<p>News Title: <input type=\"text\" name=\"news_title\" value=\"".$news_title."\"></p>";

snip

echo "<input type=\"submit\" value=\"submit\"> 
<input type=\"hidden\" name=\"submitted\" value=\"1\">
</form>";

The news ID isn't present so when the form is submitted that data isn't passed to the script, maybe try adding something like:

<input type=\"hidden\" name=\"n\" value=\"$news_id\">

generic

6:32 am on Apr 21, 2008 (gmt 0)

10+ Year Member



Ahhh ok, that did it. I could have sworn I tried that but I guess not. Thanks a ton for the help!