/    Sign up×
Community /Pin to ProfileBookmark

Database Problem with Dates

Hi.

Im sure Im doing something really stupid. I am trying to insert a date into my mysql database using the following code –

$reg_date=date();

$sql = “Insert into bb_user (username, password, city, recommended_by, status, reg_date) values (‘$username’, ‘$password’, ‘$city’, ‘$recommended_by’, ‘$status’, ‘$reg_date’)”;
$result1 = mysql_query($sql) or die(“Cannot add user!”);

The “reg_date” field is type “date”.

……… but the date shown in the database after entry is 0000-00-00

Does anybody know what Im doing wrong?

Thanks in advance.

Jonty

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@stephan_gerlachFeb 02.2009 — try this:
[code=php]
$reg_date=date('Y-m-d');
[/code]
Copy linkTweet thisAlerts:
@JontyauthorFeb 02.2009 — Hmmmm. Still doesnt seem to work. Any other suggestions?

Thanks for getting back to me.

Jonty
Copy linkTweet thisAlerts:
@JontyauthorFeb 02.2009 — Ive got the "collation", "attribute" etc columns blank. Would that cause a problem?

Thanks

Jonty
Copy linkTweet thisAlerts:
@stephan_gerlachFeb 03.2009 — Have you made usage of mysql_error()?

[code=php]
$sql = "Insert into bb_user (username, password, city, recommended_by, status, reg_date) values ('$username', '$password', '$city', '$recommended_by', '$status', '$reg_date')";
$result1 = mysql_query($sql);

if (mysql_error()!='') {
echo mysql_error();
exit;
}[/code]
Copy linkTweet thisAlerts:
@JontyauthorFeb 03.2009 — Thank you for the suggestion. Ive just been messing around with it and its started working. I got CURDATE() to work and now all of a sudden date() seems to work. Absolutely no idea what Ive done differently though!

Thanks again for all your help.

Jonty
Copy linkTweet thisAlerts:
@Phill_PaffordFeb 03.2009 — If your just getting a current date/time stamp MySQL can do this so no need for PHP to do it. Just use the NOW() in your query instead of the date variable.

[CODE]
$sql = "INSERT INTO bb_user (username, password, city, recommended_by, status, reg_date)
VALUES ('$username', '$password', '$city', '$recommended_by', '$status', NOW())";
[/CODE]
×

Success!

Help @Jonty 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 5.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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