473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to Insert curent date into mysql

I have a registration page which captures details including birth date and registration date.
The birth date and Date is not captured in mysql database.
Please help
Registration.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if (isset($_POST['Submit'])) {
  4. //$Bday  = date("dd-mm-yyyy");
  5. include "dbCon.php";
  6.  
  7. $EmailAd = $_POST['EmailAd'];
  8. $Password = $_POST['Password'];
  9. $Name = $_POST['Name'];
  10. $Age = $_POST['Age'];
  11. $Gender = $_POST['Gender'];
  12. $Bday = $_POST['Bday'];
  13. $Phone = $_POST['Phone'];
  14. $College = $_POST['College'];
  15. $Course = $_POST['Course'];
  16. $Yr = $_POST['Yr'];
  17. $UserType= $_POST['UserType'];
  18. $Address= $_POST['Address'];
  19. $Interest = $_POST['Interest'];
  20. $Affiliation = $_POST['Affiliation'];
  21. $Schedule = $_POST['Schedule'];
  22. $About = $_POST['About'];
  23. $Who = $_POST['Who'];
  24.  
  25. $query = mysql_query("INSERT into user (EmailAd, Password, Name, Age, Gender, Bday, Phone, College, Course, Yr, UserType, Address, Interest, Affiliation, Schedule, About, Who)
  26.                     VALUES('$EmailAd', '$Password', '$Name', '$Age', '$Gender', '$Bday', '$Phone', '$College', '$Course', '$Yr', '$UserType', '$Address', '$Interest', '$Affiliation', '$Schedule', '$About', '$Who')");
  27.  
  28. if ($query) {        
  29.     session_start();
  30.     $_SESSION['EmailAd'] = $_POST['EmailAd'];                                  
  31.     header ('Location: registerSuccess.php');
  32.     }
  33. else {
  34.     echo ('Failed to Input Data');
  35.     }
  36. }
  37. ?>
  38.  
Oct 5 '10 #1

✓ answered by nickweavers

To put the current date into the table for the registration date, you can do this:
Expand|Select|Wrap|Line Numbers
  1. $query="
  2. INSERT INTO table_name SET 
  3. `registration_date` = CURRENT_DATE(),
  4. `EmailAd` = '" . mysql_real_escape_string($EmailAd) . "',
  5. `Name` = '" . mysql_real_escape_string($Name) . "',
  6. `Bday` = STR_TO_DATE('".$Bday."','%d/%m/%Y')), " .
  7. ...
  8. etc
  9. ...
  10. "; 
  11.  
Using mysql_real_escape_string() will help prevent SQL injection error attacks and will also prevent things like the name O'Reilly causing syntax errors in your query.

If you would rather allow people to enter their birthday in a more commonly recognised format, you can allow
mm/dd/yy on the form and use STR_TO_DATE() to convert it for you.

Nick.

3 3570
Not sure about mysql, but in oracle I have to convert string to date format either at insert or prior
string mydate="01/01/2010";
insert into test values (to_date(mydate,'MM/DD/YYYY'));
Oct 5 '10 #2
You should use this
for birth date i have three text boxes one for day 2nd for month and 3rd for year
Expand|Select|Wrap|Line Numbers
  1. $today_date = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
  2.  
  3. $query="insert into table_name value('$today_date');
  4.  
Oct 6 '10 #3
To put the current date into the table for the registration date, you can do this:
Expand|Select|Wrap|Line Numbers
  1. $query="
  2. INSERT INTO table_name SET 
  3. `registration_date` = CURRENT_DATE(),
  4. `EmailAd` = '" . mysql_real_escape_string($EmailAd) . "',
  5. `Name` = '" . mysql_real_escape_string($Name) . "',
  6. `Bday` = STR_TO_DATE('".$Bday."','%d/%m/%Y')), " .
  7. ...
  8. etc
  9. ...
  10. "; 
  11.  
Using mysql_real_escape_string() will help prevent SQL injection error attacks and will also prevent things like the name O'Reilly causing syntax errors in your query.

If you would rather allow people to enter their birthday in a more commonly recognised format, you can allow
mm/dd/yy on the form and use STR_TO_DATE() to convert it for you.

Nick.
Oct 6 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: toedipper | last post by:
Hello, I have the following section of php code which updates a mysql record from a form. For the values of livedate and livetime I need to insert the current_date() and current_time()...
2
by: Jassim Rahma | last post by:
Hi, I am using C# to insert a date value from the DatePicker into a DateTime filed in mySQL database.. I am using the following syntax: '" + datePicker.Value + "' I also triend it withour...
3
by: raveesh | last post by:
When i try to insert a date field to Oracle DB through INSERT command, i am getting an error "Invalid Month". I am using ASP.NET 2 (VB). Please advise how to tackle this issue.
2
by: blitzztriger | last post by:
Hello!! how do i insert values into mysql , after parsing a submiting textbox?? I made the arrays, so this should be a basic insertion of them in the db, but something is missing, or in the wrong...
3
by: kanimozhiz | last post by:
Hi I am trying to insert the data in the DB(sql server2005) using the dataset. I was trying to insert the data using the stored procedures.The problem is I am unable to insert the 'date' in the...
5
ddtpmyra
by: ddtpmyra | last post by:
I don’t know if I posted my question on the right forum if not my bad, but I’m having trouble how to create a php script that will insert data on mysql Scenario: I have 3 checkbox on my form and...
0
by: brianrpsgt1 | last post by:
I am attempting to insert data from a HTML form using a .psp script. I can not find how to link the data that is inserted into the form to the variables in the .psp script to then insert into the...
3
by: gsreenathreddy | last post by:
Hi! I would like to insert the date in specified format in DB, the date type is Date Type, I am using the following queries to insert the data 1) INSERT INTO EMP VALUES (1, 'red', 'CLERK', 2,...
17
by: msmjsuarez | last post by:
Hello, I need help... How to automatically insert data to mysql database without submit button using php? Is there a way on this? thanks a lot.
2
by: yusufjammy | last post by:
How to insert image in mysql database via php and how to retrieve in php ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.