Trouble updating SQL table
Posted: Thu Jan 28, 2010 1:25 pm
I have created a simple blog and can show the comments on screen fine from the database, as well as being able to delete each one from a link. However, I am having problems updating the table and wonder if anyone has an idea of what is going wrong.
Under the blog comment I have a link called 'modify' that when clicked takes the user to a page to modify the post and update the database. My code for this is as follows:
That bit works and takes the user to the next page, which has the following code:
I get to the page to edit the post, which has the old name and old comment filled in, so I know it is getting the values. However, when I submit the form to itself to run the update code it tells me the three $_GET variables are undefined and seems to have forgotten them. Is there a way around this as I don't know why it's gone wrong?
Thanks
EDIT: Forgot to post the form code for what it matters:
Under the blog comment I have a link called 'modify' that when clicked takes the user to a page to modify the post and update the database. My code for this is as follows:
Code: Select all
$connection = mysql_connect('localhost', 'user', 'password') or die ('Unable to connect!');
mysql_select_db('database') or die ('Unable to select database!');
$result = mysql_query("SELECT * FROM blog ORDER BY date DESC");
while ($row = mysql_fetch_array($result))
{
$date = $row['date'];
$name = $row['name'];
$comment = $row['comment'];
echo '<table width="300px" border="1" cellpadding="2">';
echo '<tr>';
echo '<td colspan="2" bgcolor="#00A820">' . '<font color="#FFFFFF">' . 'Comment by ' .$row['name']. '</font>' . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2">' . 'Posted ' .$row['date']. '</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2">' .$row['comment']. '</td>';
echo '</tr>';
echo '</table>';
echo "<a href='blog_delete.php?blog=delete&date=$date&name=$name&comment=$comment'>Delete</a>";
echo "|";
echo "<a href='blog_modify.php?modify&date=$date&name=$name&comment=$comment'>Modify</a>";
}
echo '<br />';
mysql_close($connection);
Code: Select all
$oldDate = $_GET['date'];
$oldName = $_GET['name'];
$oldComment = $_GET['comment'];
if (isset($_POST['post']))
{
$newName = $_POST['name'];
$newComment = $_POST['comment'];
$connection = mysql_connect('localhost', 'user', 'password') or die ('Unable to connect!');
mysql_select_db('database') or die ('Unable to select database!');
mysql_query("UPDATE blog SET name = '$newName' WHERE name = '$oldName' LIMIT 1");
mysql_query("UPDATE blog SET comment = '$newComment' WHERE comment = '$oldComment' LIMIT 1");
mysql_close($connection);
echo '<meta http-equiv="refresh" content="0; URL=blog.php">';
}
Thanks
EDIT: Forgot to post the form code for what it matters:
Code: Select all
<h2>Modify post:</h2>
<form action="blog_modify.php" method="post">
<p><input type="text" value="<?php echo $oldName; ?>" size="45" name="name"></p>
<p><textarea name="comment" rows="8" cols="40" wrap="virtual" /><?php echo $oldComment; ?></textarea></p>
<input type="submit" name="post" value="Post">
</form>