music_fan01 Posted October 16, 2011 Share Posted October 16, 2011 I try to adjust my date and time to ("M-d-Y h:i:s A"), but when I do, I get 0's for the both the date and time. <table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><strong>Test Sign Guestbook </strong></td> </tr> </table> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form id="form1" name="form1" method="post" action="addguestbook.php"> <td> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td width="117">Name</td> <td width="14">:</td> <td width="357"><input name="name" type="text" id="name" size="40" /></td> </tr> <tr> <td>Email</td> <td>:</td> <td><input name="email" type="text" id="email" size="40" /></td> </tr> <tr> <td valign="top">Comment</td> <td valign="top">:</td> <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td> </tr> </table> </td> </form> </tr> </table> <table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td> </tr> </table> <?php $mysql_host = "myhost"; $mysql_database = "guest"; $mysql_user = "myusername"; $mysql_password = "mypassword"; //$db_name="guest"; This is not needed, as db_name is $mysql_database above, so please delete this line. $tbl_name="guestbook"; // Table name // Connect to server and select database. mysql_connect("$mysql_host", "$mysql_user", "$mysql_password") or die("cannot connect server "); mysql_select_db("$mysql_database") or die("cannot select DB"); $name = $_POST['name']; $email = $_POST['email']; $comment = $_POST['comment']; $datetime=date("y-m-d h:i:s"); //date time $sql="INSERT INTO $tbl_name (name, email, comment, datetime) VALUES ('$name', '$email', '$comment', '$datetime')"; $result=mysql_query($sql); //check if query successful if($result){ echo "Successful"; echo "<BR>"; echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page } else { echo "ERROR"; } mysql_close(); ?> <table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td> </tr> </table> <br> <?php $mysql_host = "myhost"; $mysql_database = "guest"; $mysql_user = "myusername"; $mysql_password = "mypassword"; $tbl_name="guestbook"; // Table name // Connect to server and select database. mysql_connect("$mysql_host", "$mysql_user", "$mysql_password") or die("cannot connect server "); mysql_select_db("$mysql_database") or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td>ID</td> <td>:</td> <td><? echo $rows['id']; ?></td> </tr> <tr> <td width="117">Name</td> <td width="14">:</td> <td width="357"><? echo $rows['name']; ?></td> </tr> <tr> <td>Email</td> <td>:</td> <td><? echo $rows['email']; ?></td> </tr> <tr> <td valign="top">Comment</td> <td valign="top">:</td> <td><? echo nl2br($rows['comment']); ?></td> </tr> <tr> <td valign="top">Date/Time </td> <td valign="top">:</td> <td><? echo $rows['datetime']; ?></td> </tr> </table></td> </tr> </table> <BR> <? } mysql_close(); //close database ?> Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/ Share on other sites More sharing options...
jcbones Posted October 16, 2011 Share Posted October 16, 2011 1. Make sure your database column for the datetime is set to timestamp. 2.Change your insert to: $sql="INSERT INTO $tbl_name (name, email, comment, datetime) VALUES ('$name', '$email', '$comment', CURRENT_TIMESTAMP())"; This will use MySQL's Date functions. Thereby, it will always be in the correct timestamp format. 3. Change your select to: $sql="SELECT id, name,email,comment, DATE_FORMAT(`datetime`,'%b-%d-%y %r') as datetime FROM $tbl_name"; This would be the best way, IMHO. Reference: MySQL Date Functions Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/#findComment-1279824 Share on other sites More sharing options...
Pikachu2000 Posted October 17, 2011 Share Posted October 17, 2011 And don't use short <? open tags, use the full <?php syntax. They aren't enabled by default any more, and will likely be removed entirely in a future version of php. Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/#findComment-1279834 Share on other sites More sharing options...
music_fan01 Posted October 17, 2011 Author Share Posted October 17, 2011 And don't use short <? open tags, use the full <?php syntax. They aren't enabled by default any more, and will likely be removed entirely in a future version of php. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/#findComment-1279843 Share on other sites More sharing options...
music_fan01 Posted October 17, 2011 Author Share Posted October 17, 2011 1. Make sure your database column for the datetime is set to timestamp. 2.Change your insert to: $sql="INSERT INTO $tbl_name (name, email, comment, datetime) VALUES ('$name', '$email', '$comment', CURRENT_TIMESTAMP())"; This will use MySQL's Date functions. Thereby, it will always be in the correct timestamp format. 3. Change your select to: $sql="SELECT id, name,email,comment, DATE_FORMAT(`datetime`,'%b-%d-%y %r') as datetime FROM $tbl_name"; This would be the best way, IMHO. Reference: MySQL Date Functions Change this portion $sql="SELECT * FROM $tbl_name"; to $sql="SELECT id, name,email,comment, DATE_FORMAT(`datetime`,'%b-%d-%y %r') as datetime FROM $tbl_name"; correct? Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/#findComment-1279844 Share on other sites More sharing options...
music_fan01 Posted October 17, 2011 Author Share Posted October 17, 2011 Parse error: syntax error, unexpected T_VARIABLE in /home/a5143936/public_html/addguestbook.php on line 35 (line 35 is $result=mysql_query($sql) I got the following error. <?php $mysql_host = "myhost"; $mysql_database = "guest"; $mysql_user = "myusername"; $mysql_password = "mypassword"; $comment = stripslashes($comment); $email = stripslashes($email); $name = stripslashes($name); $comment = str_replace ("<","<",$comment); $comment = str_replace ("\n","<br>",$comment); $email = str_replace ("<","<",$email); $email = str_replace ("\n","<br>",$email); $name = str_replace ("<","<",$name); $name = str_replace ("\n","<br>",$name); $tbl_name="guestbook"; // Table name // Connect to server and select database. mysql_connect("$mysql_host", "$mysql_user", "$mysql_password") or die("cannot connect server "); mysql_select_db("$mysql_database") or die("cannot select DB"); $name = $_POST['name']; $email = $_POST['email']; $comment = $_POST['comment']; $datetime=date("y-m-d h:i:s"); //date time if(empty($email) || empty($name) || empty($comment)) { echo "<h3>Sorry all fields are required</h3>"; } else { $sql="INSERT INTO $tbl_name (name, email, comment, datetime) VALUES ('$name', '$email', '$comment', CURRENT_TIMESTAMP())" $result=mysql_query($sql); //check if query successful if($result){ echo "Thank you for signing my guestbook!"; echo "<BR>"; echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page } else { echo "ERROR"; } mysql_close(); } ?> <table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td> </tr> </table> <br> <?php $mysql_host = "myhost"; $mysql_database = "guest"; $mysql_user = "myusername"; $mysql_password = "mypassword"; $tbl_name="guestbook"; // Table name $comment = stripslashes($comment); $email = stripslashes($email); $name = stripslashes($name); $comment = str_replace ("<","<",$comment); $comment = str_replace ("\n","<br>",$comment); $email = str_replace ("<","<",$email); $email = str_replace ("\n","<br>",$email); $name = str_replace ("<","<",$name); $name = str_replace ("\n","<br>",$name); // Connect to server and select database. mysql_connect("$mysql_host", "$mysql_user", "$mysql_password") or die("cannot connect server "); mysql_select_db("$mysql_database") or die("cannot select DB"); $sql="SELECT id, name,email,comment, DATE_FORMAT(`datetime`,'%b-%d-%y %r') as datetime FROM $tbl_name"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td>ID</td> <td>:</td> <td><? echo $rows['id']; ?></td> </tr> <tr> <td width="117">Name</td> <td width="14">:</td> <td width="357"><? echo $rows['name']; ?></td> </tr> <tr> <td>Email</td> <td>:</td> <td><? echo $rows['email']; ?></td> </tr> <tr> <td valign="top">Comment</td> <td valign="top">:</td> <td><? echo nl2br($rows['comment']); ?></td> </tr> <tr> <td valign="top">Date/Time </td> <td valign="top">:</td> <td><? echo $rows['datetime']; ?></td> </tr> </table></td> </tr> </table> <BR> <? } mysql_close(); //close database ?> Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/#findComment-1279848 Share on other sites More sharing options...
Pikachu2000 Posted October 17, 2011 Share Posted October 17, 2011 Look at the end of the preceding line. Notice anything missing? Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/#findComment-1279857 Share on other sites More sharing options...
silkfire Posted October 17, 2011 Share Posted October 17, 2011 You forgot your semicolon, mate! Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/#findComment-1279892 Share on other sites More sharing options...
music_fan01 Posted October 17, 2011 Author Share Posted October 17, 2011 Thanks for the help! I was thinking it something worse wrong with that line. Quote Link to comment https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/#findComment-1280076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.