Jump to content

Timestamps, Forms, and MySQL...o my!


Superman702

Recommended Posts

Okay. Thanks to other who helped me so far. I am almost done, but totally confused with this timestamp issue and MySQL. I haven't used HTML in awhile and decided to jump into PHP. I like PHP because it reminds me of C++, but please bare with me with the newbie questions. I have three files:

 

test php:

 <html>
<head>
<title>PHP Test Page</title>
</head>
<body>
<form action="process.php" method="post"> 
Message: <input type="text" name="Main"><br> 
<input type="submit" value="Submit"> 
</form> 
</body>
</html>

 

process.php

<? 
$Main=$_POST['Main']; 
mysql_connect("*****", "*****", "*****") or die(mysql_error()); 
mysql_select_db("*****_test") or die(mysql_error()); 
mysql_query("INSERT INTO `Message` VALUES ('$Main')"); 
Print "Your message will now be broadcasted."; 
?> 

 

admin.php

<HTML>
<HEAD>
<TITLE>v2.0</TITLE>
<meta http-equiv="refresh" content="20"> 
</head>
<BODY>
<?php 
// Connects to your Database 
$link = mysql_connect("*****", "*****", "*****"); 
mysql_select_db("*****_test", $link); 
$result = mysql_query("SELECT * FROM Message", $link); 
$num_rows = mysql_num_rows($result); 
if($num_rows == 0){ 
echo "<table width=\"100%\" border=\"0\"> "; 
echo "<tr><td width=\"10%\"><CENTER>LA</CENTER></td>"; 
echo "<td width=\"80%\"><CENTER>Order Menu</CENTER></td>"; 
echo "<td><CENTER>Blank</CENTER></td>"; 
echo "</TR></table>"; 
} 
else{ 
$info = mysql_fetch_assoc($result);
echo "<table width=\"100%\" border=\"0\"> "; 
echo "<tr><td width=\"10%\"><CENTER>LA</CENTER></td>"; 
echo "<td width=\"80%\" bgcolor=\"red\"><marquee behavior=\"scroll\" direction=\"left\"><B>IMPORTANT MESSAGE: </B>".$info['Main'] . "</marquee></td>"; 
echo "<td><CENTER>Blank</CENTER></td>"; 
echo "</TR></table>"; 
}

?> 
</BODY>
</HTML>

 

MySQL 2.11.4

 

Message = Table Name

Main varchar(500) = column #1

TimeStamper (datetime) = column #2

 


 

I want the current time of the submission of the form to be included with process.php, but I have no idea what I am doing with this whole time() function. I don't have it in the example above, but it was a mess and I guess I made up a lot of stuff that MySQL hasn't heard of before.

 

What do I need to add to process.php to include a datetime value for Timestamper?

Link to comment
Share on other sites

time will basically return the number of seconds that have passed since 1st Jan 1970. You can convert any timestamp into other date formats using date, but if all you want is to store the time of the operation in your database, you can also use a mysql timestamp (that will automatically update the field for you, no extra code needed.
Link to comment
Share on other sites

When I submit the form, no entry is created. The form seems to have went thru with no errors, but when I check the table it says:

 

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0001 sec)

 

However, if I remove the Timestamper column, the form does submit and an entry is created. Just of course, with no timestamp though. :-\

Link to comment
Share on other sites

wait...I think I have something going here....just got it to go thru with this

 

<? 
$Main=$_POST['Main']; 
$Timestamper=time();
$gettime=time();
mysql_connect("****", "****", "****") or die(mysql_error()); 
mysql_select_db("a7405553_test") or die(mysql_error()); 
mysql_query("INSERT INTO `Message` VALUES ('$Main', '$Timestamper')"); 
Print "Your message will now be broadcasted."; 
?> 

 

But the value comes up with all zeros in the table. I think I am close though!!!

Link to comment
Share on other sites

Why 2 variables doing the same thing?

$Timestamper=time();
$gettime=time();

 

Yes, you can create your timestamp in php and send it through, but all this can also be automatic:

in phpmyadmin,  change field type from datetime to timestamp, and set Default value to CURRENT_TIMESTAMP, and Attributes to on update CURRENT_TIMESTAMP

 

now your timestamp will update automatically every time a new database entry is created or modified.

Link to comment
Share on other sites

I forgot to delete that other var. I got rid of it now

 

I changed the Timestamper column to Timestamper timestamp  ON UPDATE CURRENT_TIMESTAMP No CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP. It still stills the default at all zeros. I am going to log into my other computer to take a print screen. I don't have that button on my labtop. Until then though, this is my new process.php file.

 

<?

$Main=$_POST['Main'];

$Timestamper=time();

mysql_connect("*****", "*****", "*****") or die(mysql_error());

mysql_select_db("*****_test") or die(mysql_error());

mysql_query("INSERT INTO `Message` VALUES ('$Main', '$Timestamper')");

Print "Your message will now be broadcasted.";

?>

Link to comment
Share on other sites

there's nothing wrong with doing it through php, as you have, so I don't really want to waste your time too much on this. It's just a way of having things a little more automated so you don't have to script in a timestamp update every time you change stuff in the database.

check out my print screen:

printscreen2.gif

 

Link to comment
Share on other sites

So it turns out that when you use the time() function, it creates a int. rather than the timestamping code. So when I changed Timestamper to an INT rather then TIMESTAMP, I got a value.

 

Untitled3.jpg

Untitled4.jpg

 

I think I found exactly what I needed. How do I get a value for TIMESTAMP I guess is what I am really trying to ask. 8)

Link to comment
Share on other sites

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.