Jump to content

Recommended Posts

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
?>

Link to comment
https://forums.phpfreaks.com/topic/249229-guestbook-time-shows-up-as-0/
Share on other sites

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

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?

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.  :confused:

 

 

<?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
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.