Jump to content

Entering date into mysql table


slj90

Recommended Posts

I want to enter the date that a user signs up..

 

Here is the code I use to enter the users details into the user table in my database...

 

$query = "INSERT INTO Customer (Firstname, Surname, Username, Email, Password, Address, PostCode, City, Country) VALUES ('$newFirstname', '$newSurname', '$newUsername', '$newEmail', '$newPassword', '$newAddress', '$newPostCode', '$newCity', '$newCountry')"; 

 

What would I put to insert the date aswel? The date field in my table is 'SDate'.

 

Thanks alot

Link to comment
https://forums.phpfreaks.com/topic/188877-entering-date-into-mysql-table/
Share on other sites

You just define a variable called $date and add it to your INSERT code

$query = "INSERT INTO Customer ('Firstname', 'Surname', 'Username', 'Email', 'Password', 'Address', 'PostCode', 'City', Country date) VALUES ('$newFirstname', '$newSurname', '$newUsername', '$newEmail', '$newPassword', '$newAddress', '$newPostCode', '$newCity', '$newCountry', '$date')";

 

Make sure you have a date column in your database called date with a value of date

Hi bullbreed, I have done what you said and it doesn't appearing to be working.

 

The field in my table is a 'date' field

 

Here is my code now:

 

$date = date("YYYY-MM-DD");
   
$query = "INSERT INTO Customer (Firstname, Surname, Username, Email, Password, Address, PostCode, City, Country, SDate) VALUES ('$newFirstname', '$newSurname', '$newUsername', '$newEmail', '$newPassword', '$newAddress', '$newPostCode', '$newCity', '$newCountry', '$date')"; 

 

Thanks for your help.

 

 

If you're interested in inserting the current date or current date/time and you are using MySQL (although I'm sure the other database engines have equivalents) why not use the NOW() or CURRENT_DATE mysql variables.

 

Just in case you're not sure what I mean:

 

insert into news_item(news_id, title, create_date) values (1, 'Test News Item', NOW())

OR

insert into news_item(news_id, title, create_date) values (1, 'Test News Item', CURRENT_DATE)

 

Hope that helps.....

Josh

\is your code inserting anything in to the database?

 

Also your date column in your MySQL database shoul dbr set like this;

Field---Type---Collation---Attributes---Null---Default---Extra--- Action

 

date    date                   No    None

 

Are you checking the form data and defining variables

<?php
$submit = $_POST['submit']; - (This is the name of your form)

//Form data
$description = addslashes(strip_tags($_POST['description']));
$all your other variables here
$date = date("Y-m-d");

if ($submit){

//Check for existing fields
if ($description, $other variables, ){
        
	//Register the user
	$queryreg = mysql_query("
  INSERT INTO Customer  (`description`, `other variables`, `date`)   VALUES ('$description', `$other variables` $date')");

?>

have a play around with it but use the quotes around your inserts and intos

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.