Hello all.
I have recently starting programming with PHP about month ago. I have been working on a simply guestbook script that will allow vistors to post comments to a article that was wrote on my webpage. I have finally got the coding correct. The problem I have ran into is converting MySQL time date to PHP time date and visa versa. I have been hunting around for help in it and finally settled on using MySQL function DATE_FORMAT to perform the task I needed. I got it to work like I wanted to. The only problem is now when I tried created a couple of dummy comment posts it now only shows me the first post I created each and everytime I post a new comment. For example if I posted something like:
Current Comments:
Name: JIM
Comments: hi testing the website
Posted: Friday, July 06, 2007 at 3:51 AM
Each time I post a new one it just creates a new posting in the browser as the same thing from above. The odd thing is that when I go in to PhpMyAdmin it shows the correct post that I made. So it seems like PHP is only displaying the first post I create. I have deleted the database several times and still the same results.
FYI I was able to get the Name and Comments sections working just find but when I put the follow two functions back into the code it displays and updates the time but only give me the one name and comment posts from above.
$querydate = mysql_query ("SELECT DATE_FORMAT(date,'%W, %M %d, %Y at %l:%i %p') AS date FROM comments1",$link);
while ( $row1 = mysql_fetch_array( $querydate, MYSQL_ASSOC ) )
Here is my whole script so maybe someone can figure out why it is messing up
<?PHP
/*bulletin.php
Basic Quest book
This is a script that allows people to
leave comments on a web page.
*/
require "db_guestbook.php";
$link = mysql_connect( $db_host, $db_username, $db_password ); // connection to database
if ( !$link )
exit ( "Database error: " . mysql_error() );
mysql_select_db ( $db_name, $link ); // selects database name
$me = $_SERVER['PHP_SELF'];
if ( isset ( $_POST['comments'] ) ) // check for current comments
{
$comments = $_POST ['comments'];
$name = $_POST ['name'];
$date = date('Y-m-d h:i:s'); //php method
$result = mysql_query( "INSERT INTO comments1 (name, comments, date) VALUES('$name', '$comments','$date' )", $link ); // insert post into db
if( !$result )
exit ( "Query failed: " . mysql_error() );
header("Location: $_SERVER[PHP_SELF]?$_SERVER[QUERY_STRING]");//this where you redirct to the same page
}
else
{
print ( "<b>Current Comments: </b><br>\n" ) ;
mysql_select_db ( $db_name, $link ); // selects database name
$querydate = mysql_query ("SELECT DATE_FORMAT(date,'%W, %M %d, %Y at %l:%i %p') AS date FROM comments1",$link); //retrieve date
$result = mysql_query ("SELECT name,comments FROM comments1", $link ); //Retrieve Info
if ( $result )
{
while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) // variable to hold name and comments
while ( $row1 = mysql_fetch_array( $querydate, MYSQL_ASSOC ) ) // fetch variable to hold date
print ( "<i>Name</i>: " . $row["name"] . "<br> <i>Comments</i>: " . $row["comments"] .
"<br>\n " . " <i>Posted</i>: " . $row1["date"] ."<br><br><hr />" );// $row prints name and comments // $row1 print datetime
}
else
exit ( "Query failed: " . mysql_error() );
print( "<br><b>Add a comment: </b><br>\n" );
// user interaction form
print( "<form action=\"$me\" method=\"post\">\n" );
print( "Your name:");
print( "<input type=\"text\" name=\"name\" size=\"20\"/><br>\n" );
print( "Your comments:" );
print( "<input type=\"text\" name=\"comments\" size=\"50\" /><br>\n");
print( "<input type=\"submit\" value=\"Submit\"/>\n" );
print( "<input type=\"reset\" value=\"Reset\" />");
print( "</form>\n" );
}
//mysql_close( $link ); // close db connection
?>
I'm still very new to PHP so forgive my coding if it looks bad
my msn messenger is shanefergie@hotmail.com if any wants to help me with this.