I made an update function & a form to update a page, but it is inserting a new row into the db instead of updating it. I can’t for the life of me figure out where I’ve gone wrong. Can anyone spot my mistake? Thanks
url looks like this: update.php?id=12
function update_content($params)
{
$id = $_GET['id'];
$connection = db_connect();
$query = sprintf("UPDATE content SET page = '%s',
page_id = '%s',
title = '%s',
text = '%s'
WHERE id = $id;",
mysql_real_escape_string($params['page']),
mysql_real_escape_string($params['page_id']),
mysql_real_escape_string($params['title']),
mysql_real_escape_string($params['text'])
);
$result = mysql_query($query);
if (!$result)
{
return false;
}
else
{
return true;
}
}
// Grab the content to echo into the title & text fields
function show_content()
{
$id = $_GET['id'];
$connection = db_connect();
$query = sprintf("select * from content where id = $id");
$result = mysql_query($query);
$number_of_posts = mysql_num_rows($result);
if ($number_of_posts == 0)
{
return false;
}
$row = mysql_fetch_array($result);
return $row;
}
if(isset($_POST['text']))
{
$result = update_content($_POST);
}
?>
And the form…
<form name="content" id="content" method="post" action="add-content.php">
<p><br />
<b>Assign to page</b><br />
<input name="page" id="page" size="60" maxlength="500" value="<?php
// Get the name from the url and put the space back
$page = show_content ();
echo $page['page'];
?>" />
<br />
<br />
<b>Page id</b>
<input name="page_id" id="page_id" size="4" maxlength="4" value="<?php
// Get the id from the url
$page_id = show_content ();
echo $page_id['page_id'];
?>
" />
<br />
<br />
<b>Title</b>
<span class="smalltext">(Just a short name this piece of content)</span><br />
<input name="title" id="title" size="60" maxlength="200" value="<?php
// Get the name from the url and put the space back
$title = show_content ();
$page = str_replace('-', ' ',$page);
echo $page['title'];
?>" />
<br />
<br />
<br />
<strong>Content</strong> <span class="smalltext">(paste html in here)</span><br />
<textarea name="text" id="text" cols="75" rows="15"><?php $id = $_GET['id'];
$text = show_content ();
echo $text['text'];
?></textarea>
<?php //turn the text area into CK Editor echo $ckeditor_ini; ?>
<br />
<input type="image" src="../images/button_submit.gif" alt="submit" name="submit" value="submit" />
<a href="index.php"><img src="../images/button_cancel.gif" alt="Cancel" width="120" height="26" border="0" /></a></p>
</form>