tags:

views:

41

answers:

4

I've created a form that submits data to a MySQL database but the Date, Time, Year and Month fields constantly revert to the exact same date (1st January 1970) despite the fact that when I submit the information to the database the form displays the current date, time etc to me. I've already set it so that the time and date fields automatically display the current time and date. Could someone please help me with this.

Form:

    <html>

<head>
<title>Ultan's Blog | New Post</title>
<link rel="stylesheet" href="css/newposts.css" type="text/css" />
</head>

<body>
<div class="new-form">
<div class="header">
<a href="edit.php"><img src="images/edit-home-button.png"></a>
</div>
<div class="form-bg">
<?php
if (isset($_POST['submit'])) {

    $month = htmlspecialchars(strip_tags($_POST['month']));
    $date = htmlspecialchars(strip_tags($_POST['date']));
    $year = htmlspecialchars(strip_tags($_POST['year']));
    $time = htmlspecialchars(strip_tags($_POST['time']));
    $title = htmlspecialchars(strip_tags($_POST['title']));
    $entry = $_POST['entry'];

        $timestamp = strtotime($month . " " . $date . " " . $year . " " . $time);

    $entry = nl2br($entry);

    if (!get_magic_quotes_gpc()) {
        $title = addslashes($title);
        $entry = addslashes($entry);
    }

    mysql_connect ('localhost', 'root', 'root') ;
    mysql_select_db ('tmlblog');

$sql = "INSERT INTO php_blog (timestamp,title,entry) VALUES ('$timestamp','$title','$entry')";

    $result = mysql_query($sql) or print("Can't insert into table php_blog.<br />" . $sql . "<br />" . mysql_error());

    if ($result != false) {
        print "<p class=\"success\">Your entry has successfully been entered into the blog. </p>";
    }

    mysql_close();
}
?>

<?php
$current_month = date("F");
$current_date = date("d");
$current_year = date("Y");
$current_time = date("H:i");
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<input type="text" name="month" value="<?php echo $current_month; ?>" />
<input type="text" name="date" value="<?php echo $current_date; ?>" />
<input type="text" name="year" value="<?php echo $current_year; ?>" />


<input type="text" name="time" id="time" size="5"value="<?php echo $current_time; ?>" />

<input class="field2" type="text" id="title" value="Title Goes Here." name="title" size="40" />

<textarea class="textarea" cols="80" rows="20" name="entry" id="entry" class="field2"></textarea>

<input class="field" type="submit" name="submit" id="submit" value="Submit">

</form>
</div>
</div>
 </div>
 <div class="bottom"></div>
 <!-- //End Wrapper!-->  
</body>

</html>

</html>

alt text

For some reason the posts are being submitted without a timestamp and are reverting to a default timestamp.

A: 

Looks like strtotime don't understand the format you feeding to it.
Use mktime instead.

Anyway you have to always debug your code, i.e. print out the query to see if anything goes wrong.

Or consider to use mysql-supported type colymn, date or datetime

You may be confused an unix timestamp which is just number ans mysql timestamp type column which is string of 2010-05-01 00:00:00

Col. Shrapnel
I've tried that now but now it still displays the current date and time but submits as July 23rd 2010.
ThatMacLad
@ThatMacLad what type of timestamp field in your database? if it isn't `int` you shouldn't store unix timestamp there
Col. Shrapnel
I've updated this question please check out the new information... please help.
ThatMacLad
+1  A: 

Sounds like your form is giving your database a very low amount of "Ticks". Notice you have an with name="date" and id="date" three times. This is most likely your problem. Change those names and ids, do use month, year, date (according to your "postback").

Lucas
I've updated this question please check out the new information... please help.
ThatMacLad
+1  A: 

You have three text boxes named date. Since $_POST['month'] and $_POST['year'] are not set, strtotime() cannot figure out the correct timestamp and the SQL query looks like this:

INSERT INTO php_blog (timestamp,title,entry) VALUES ('','Title Goes Here.','')

Change the names of the input elements to what your PHP code is expecting:

<input type="text" name="month" value="<?php echo $current_month; ?>" />
<input type="text" name="date" value="<?php echo $current_date; ?>" />
<input type="text" name="year" value="<?php echo $current_year; ?>" />

After you've done this, strtotime() will be able to parse the date and insert the correct value into the query.

INSERT INTO php_blog (timestamp,title,entry) VALUES ('1272738360','Title Goes Here.','')
Mike
I've updated this question please check out the new information... please help.
ThatMacLad
But you didn't fix your form. After fixing it I ran your code on my machine and a correct timestamp was generated.
Mike
I didn't fix the code on here, I added an image that shows how it's submitting a timestamp of '0'. I fixed the code on my computer. I've updated the form yet still it doesn't submit to my blog with the correct timestamp.
ThatMacLad
I rewrote the whole page but included you're solution and this corrected the problem. Thanks Mike!
ThatMacLad
A: 

It could be that strtotime fails to recognize the format you are submitting your date in.

Try a classic YYYY/MM/DD format:

 $timestamp = strtotime("$year/$month/$date $time");
Pekka
I've updated this question please check out the new information... please help.
ThatMacLad
@ThatMacLad yes, January 1st 1970 is timestamp `0`. Have you tried my suggestion or one of the others?
Pekka
I've tried all of the previous suggestions. Your suggestion seems to set the default timestamp to July 23rd 2010.
ThatMacLad
@ThatMacLad it could be that `strtotime` interprets the input wrongly. The safest approach would be the `mktime` one outlined by Col. Shrapnel.
Pekka
@ThatMacLad with what input data do you get July 23rd?
Pekka
I've tried that but it seems to submit the incorrect date, time etc. still. I'll try it again now and I'll get back to you. I do believe that it may be that it's not submitting the information at all. Should I post the php that I used to create the php_blog table here?
ThatMacLad
@ThatMacLad no, a clean output of what actually gets transmitted is the first step: `echo $_POST["date"];` etc.
Pekka
I'm currently entering the content using the edited form thanks to the post here by Mike. The form is still displaying the current date when I submit it. It doesn't appear to be submitting it to the database though.
ThatMacLad
@ThatMacLad then start doing step-by-step test outputs. What value does `$timestamp` have? If it's always `0`, then what value do the form fields have? Feel free to update your question with your current code.
Pekka
What exactly is it that you want me to do?
ThatMacLad
@ThatMacLad echo out the values. If the timestamp is 0, something goes wrong at some point. It is not clear yet at which point. Echo out `echo $timestamp;` and see what it contains, etc. I'm off for today, good luck!
Pekka