Hey everyone. I'm new here and let me apologize if I am reposting a question; I've looked through the forums for a while and can't seem to find anything similar.
I'm using Jono Bacon's "Practical PHP and MySQL with Applications" and working my way through his blog project. All seems good and well, but I got to the part about submitting new comments on blog posts. When I click the submit button, it should post the various information to the MySQL database. It does this, but the submit button doesn't refresh the page. It does submit the mysql query, so if I manually reload, the new comment shows up, but the submit button doesn't cause the page to refresh automatically.
Below is my code. Any help is greatly appreciated.
<?php
require("config.php");
if(isset($_GET['id']) == TRUE) {
if(is_numeric($_GET['id']) == FALSE) {
$error = 1;
}
if($error == 1) {
header("Location: " . $config_basedir);
}else{
$validentry = $_GET['id'];
}
}else{
$validentry = 0;
}
//check to see if this page is being loaded as the result of a comment being submitted
if($_POST['submit']){
$db = mysql_connect($dbhost, $dbuser, $dbpassword); //appears to return a pointer to the database
mysql_select_db($dbdatabase, $db);
$sql = "INSERT INTO comments(blog_id, tier, replyto, dateposted, name, comment) VALUES(" . $validentry . ", 1, 0, NOW(), '" . $_POST['name'] . "', '" . $_POST['comment'] . "');";
mysql_query($sql);
header("Location: http://" . $HTTP_HOST . $SCRIPT_NAME . "?id=" . $validentry);
}else{
//code goes here
}
require("header.php");
if($validentry == 0) {
$sql = "SELECT entries.*, categories.cat FROM entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1;";
}else{
$sql = "SELECT entries.*, categories.cat FROM entries, categories WHERE entries.cat_id = categories.id AND entries.id = " . $validentry . " ORDER BY dateposted DESC LIMIT 1;";
}
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo "<h2>" . $row['subject'] . "</h2><br>";
echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] . "'>" . $row['cat'] . "</a> - Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted'])) . "</i>";
echo "<p>";
echo nl2br($row['body']);
echo "</p>";
//form the SQL; we only want top level comments as these are relpies to the post itself
$commsql = "SELECT * FROM comments WHERE blog_id = " . $validentry . " AND tier = 1 ORDER BY dateposted DESC;";
//now send the query
$commresult = mysql_query($commsql);
//now count how many comments we have
$num_commrows = mysql_num_rows($commresult);
echo "<div id='comment'>";
if ($num_commrows == 0) {
echo "<p><i>No comments.</i></p>";
}else{
$i = 0;
//the take the result and put it into an array - use this as the sentinel control
while($commrow = mysql_fetch_assoc($commresult)) {
//first set up the formatting tag. Build a table
echo "<table><tr class='commentinfo'><td class='commentauthor'>";
//post the comment info; name and date
echo "<a name='comment" . $i . "'>";
echo "Comment by " . $commrow['name'] . "</a></td>";
//next cell, put the date on the far right
echo "<td class='commentdate'>";
echo date("D jS F Y g.iA", strtotime($commrow['dateposted'])) . "</td></tr><tr><td class='commentbody' colspan=2>";
echo $commrow['comment'];
echo "</td></tr>";
//stick in the reply button;
echo "<tr class='commentoptions'><td colspan=2>[reply]</td></tr>";
echo "</table>";
//now we stick in relpies to the comments
$repsql = "SELECT * FROM comments WHERE blog_id = " . $validentry . " AND tier = 2 AND replyto = " . $commrow['id'] . " ORDER BY dateposted DESC;";
$represult = mysql_query($repsql);
$num_reprows = mysql_num_rows($represult);
if ($num_reprows > 0) {
$j = 0;
//the take the result and put it into an array - use this as the sentinel control
while($reprow = mysql_fetch_assoc($represult)) {
//first set up the formatting tag. Build a table
echo "<table style='margin-left:50px;'><tr class='commentinfo'><td class='commentauthor'>";
//post the comment info; name and date
echo "Comment by " . $reprow['name'] . "</td>";
//next cell, put the date on the far right
echo "<td class='commentdate'>";
echo date("D jS F Y g.iA", strtotime($reprow['dateposted'])) . "</td></tr><tr><td class='commentbody' colspan=2>";
echo $reprow['comment'];
echo "</td></tr>";
echo "</table>";
}
}
$i++;
}
}
//close off the div tag
echo "</div>";
?>
<h3>Leave a comment</h3>
<form action="<?php echo $_SERVER['SCRIPT_NAME'] . "?id=" . $validentry; ?>" method="post">
<table>
<tr>
<td>Your name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Comments:</td>
<td><textarea name="comment" rows="10" cols="50"></textarea></td>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Add comment"></td>
</tr>
</table>
</form>
<?php
require("footer.php");
?>