/    Sign up×
Community /Pin to ProfileBookmark

mysql timestamp only showing 0000-00-00

I am trying to make an automating news script. I want it to display when the script was submitted. Also, the order in which the news is displayed is relying on the timestamp. The timestamp is only showing 0000-00-00.

heres url to site[url]http://www.uogameresources.com/sik[/url]

This code is in the news page.

[code]
$query = mysql_query(“select id,name,title,text,DATE_FORMAT(postdate, ‘%Y-%m-%d’) as date from news order by postdate DESC limit 6”);
while($news = mysql_fetch_array($query)) {
echo “<div class=’n_title’>Title: “. $news[‘title’]. “<br>”;
echo “Posted by: “.$news[‘name’].” on “.$news[‘date’].”</div>”;
echo $news[‘text’]. “<br><br>”;
[/code]

This code is in the form page.

[code]
mysql_query(“insert into news (name,title,postdate,text) values (‘$name2′,’$title’,’postdate’,’$text’)”);
[/code]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@pyroSep 25.2003 — It looks like it could be your insert command. postdate is probably supposed to be $postdate...
Copy linkTweet thisAlerts:
@gokouauthorSep 25.2003 — Nope, that wasn't it. Maybe I should of showed the form code?

Am I suppose to add a hidden field to insert the postdate? I don't see why I would though.

<i>
</i>&lt;html&gt;
&lt;body&gt;
Submit News&lt;br /&gt;
&lt;form name="form2" method="post"&gt;
Name:&lt;input type="text" name="name2"&gt;&lt;br /&gt;
Title:&lt;input type="text" name="title"&gt;&lt;br /&gt;
Text:&lt;textarea name="text" rows="5"&gt;&lt;/textarea&gt;&lt;br /&gt;
&lt;input type="submit" value="submit"&gt;
&lt;input type="reset" value="reset"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@pyroSep 25.2003 — In the code you originally posted, how is the date suppose to be filled out? I was assuming you would want something like this:

[code=php]
$name2 = $_POST['name2'];
$title = $_POST['title'];
$postdate = time(); #returns current unix timestamp
$text = $_POST['text'];
mysql_query("insert into news (name,title,postdate,text) values ('$name2','$title','$postdate','$text')");[/code]
Copy linkTweet thisAlerts:
@gokouauthorSep 26.2003 — Sorry. I made a mistake. I don't need to insert it.

I made a mysql timestamp field called postdate. That field is already being used on the page, but it's showing 0000-00-00 instead of a real date.
Copy linkTweet thisAlerts:
@pyroSep 26.2003 — Look in the database. I'd be willing to bet it is incorrect there as well, which means it is being inserted wrong.
Copy linkTweet thisAlerts:
@gokouauthorSep 26.2003 — <i>
</i>CREATE TABLE news (
id int(20) NOT NULL auto_increment,
postdate timestamp(14),
name varchar(50) NOT NULL,
title varchar(100) NOT NULL,
text text NOT NULL,
PRIMARY KEY (id)
);


That's my table code. I don't think theres anything wrong.
Copy linkTweet thisAlerts:
@pyroSep 26.2003 — Hmm... Yes, if you are using a timestamp field, it should be automatically updated. Try this:

[code=php]mysql_query("insert into news (name,title,postdate,text) values ('$name2','$title','','$text')");[/code]
Copy linkTweet thisAlerts:
@gokouauthorSep 26.2003 — I thought that was gonna work, but didn't. Maybe it's how I selected it.
Copy linkTweet thisAlerts:
@Daot_LagorilleSep 26.2003 — Yowie.

I have been doing similar things and there are two ways I have done this:

First, I declare variables:

$varCurrentdte = date ("Y-m-d");

$CurrentTime = date ("h:i");

Then, I use hidden fields in the submission form, with the values set to:

<?php echo $CurrentTime; ?> (and the other one too)

These are inserted into the appropriate fields in the database.

The other way, which I think is what you want to do, is that I have a field in the mysql database with the type set to "TIMESTAMP".

In this case, you need to insert nothing into that field - the field in each record will have the value of whenever it was created, and look like this:

2003080512372

It goes year, month, day, minutes, seconds...

Helps?
Copy linkTweet thisAlerts:
@Daot_LagorilleSep 26.2003 — [code=php]mysql_query("insert into news (name,title,postdate,text) values ('$name2','$title','','$text')");[/code]

Problem with this code is that you have the timestamp field in the insert line. Don't. Then you will be fine, I think.
×

Success!

Help @gokou spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...