Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Insert dates into a table from html form

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Insert dates into a table from html form

Postby okoa » Thu May 09, 2013 5:42 am

Hi team!

I am feeding data into a database using a html form with date text input field. I don't want to enter the current date as what I am recording happened in the previous but I still need to calculate the number of days, months and years since the event occured. I have writen the following code to insert date into the database table:

[b]$event_date=mysql_real_escape_string($_POST['date']);
$event_date=date('Y-m-d,strtotime($event_date));
$insert_date=maysql_query("insert into table_name(date_field)VALUES('$event_date')";
[/b]

surpringly, the insert query is inserting 0000-00-00. The user is entering date in m-d-yyyy format. I have validated the user input date using the following code:
$date_arr = explode('-', $event_date);
if (checkdate($date_arr[0], $date_arr[1], $date_arr[2])==false) {
// valid date ...
$errors[]="What you entered is not a valid date format";
}

the validation have worked fine.

This is my question: What is going wrong in that the query is inserting 0000-00-00 instead of the date value? When echo the $event_date before inserting into the database is like is working fine. Any help please.
thank you in advance.
okoa
New php-forum User
New php-forum User
 
Posts: 8
Joined: Wed May 08, 2013 9:18 am

Re: Insert dates into a table from html form

Postby sandeep605085 » Thu May 09, 2013 11:27 am

Hi,

Did you try echo the $event_date value after getting it from date('Y-m-d,strtotime($event_date));

if
below is your correct code
$event_date=date('Y-m-d,strtotime($event_date));
then
i guess the problem is because of missing single quote(') for "Y-m-d" format in date('Y-m-d,strtotime($event_date));

and it should be
$event_date=date('Y-m-d',strtotime($event_date));

Thanks,
Sandeep Agrawal
Mindfire Solutions.
User avatar
sandeep605085
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions

Re: Insert dates into a table from html form

Postby okoa » Thu May 09, 2013 8:17 pm

Thanks for your reply, omision of the quote is a mistake.made in this posting. In my code i had echo $event_date and it printed fine. the problem is, it is not inserting the value into the database.
okoa
New php-forum User
New php-forum User
 
Posts: 8
Joined: Wed May 08, 2013 9:18 am

Re: Insert dates into a table from html form

Postby sandeep605085 » Thu May 09, 2013 9:37 pm

Hi,

Can you please post here the echo of "insert into table_name(date_field)VALUES('$event_date')";
and the database table_name structure so that i can analyse and fix it.



Thanks,
Sandeep Agrawal
Mindfire Solutions.
User avatar
sandeep605085
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions

Re: Insert dates into a table from html form

Postby okoa » Fri May 10, 2013 7:18 am

This is the code:
$event_date=mysql_real_escape_string($_POST['date']);
$event_date= date('Y- m- d', strtotime($event_date));
echo $event_date;
//output eaxmple
2013-4-23
//insert query

$query_insert_date=mysql_query("INSERT INTO admision(Registrationid,Classid,studentid,Fee,AdminNumber,Date) VALUES('','$classid','$studentid','$fee','$adminnumber','$event_date')")
Records for other fields inserts correctly except for the date which inserts 0000-00-00
the date field is set to date date type in the database. When echo the variable $event_date prints what I was exptecting that parodox is what is being inserted in the table.

Thanks once again.
okoa
New php-forum User
New php-forum User
 
Posts: 8
Joined: Wed May 08, 2013 9:18 am

Re: Insert dates into a table from html form

Postby sandeep605085 » Fri May 10, 2013 9:32 am

Code looks fine, datatype is also fine. its amazing why not working.

if you can post the output of
echo "INSERT INTO admision(Registrationid,Classid,studentid,Fee,AdminNumber,Date) VALUES('','$classid','$studentid','$fee','$adminnumber','$event_date')";

and the database table structure for table admission.
that is the query to create the table admission like datatype, length,key all that. then i may be i can find the issue.

Thanks,
Sandeep Agrawal
Mindfire Solutions.
User avatar
sandeep605085
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions

Re: Insert dates into a table from html form

Postby okoa » Fri May 10, 2013 10:12 pm

the query produces the following output:

echo "INSERT INTO admision(Registrationid,Classid,studentid,Fee,AdminNumber,DOA,SerialNumber) VALUES('','$classid','$studentid','$fee','$adminnumber','$event_date', '$serial)";
INSERT INTO admision(Registrationid,Classid,studentid,Fee,AdminNumber,Date,SerialNumber) VALUES('','100','12','2000','0012','\'1969- 12- 31\'', '4981-2721-2541-61, 4981-2721-2541-61\r\n')


this is my table structure:


)
Thanks once again
okoa
New php-forum User
New php-forum User
 
Posts: 8
Joined: Wed May 08, 2013 9:18 am

Re: Insert dates into a table from html form

Postby okoa » Sun May 12, 2013 9:03 am

the query produces the following output:

echo "INSERT INTO admision(Registrationid,Classid,studentid,Fee,AdminNumber,DOA,SerialNumber) VALUES('','$classid','$studentid','$fee','$adminnumber','$event_date', '$serial)";
INSERT INTO admision(Registrationid,Classid,studentid,Fee,AdminNumber,Date,SerialNumber) VALUES('','100','12','2000','0012','\'1969- 12- 31\'', '4981-2721-2541-61, 4981-2721-2541-61\r\n')

this is my table structure:


)
Thanks once again
okoa
New php-forum User
New php-forum User
 
Posts: 8
Joined: Wed May 08, 2013 9:18 am


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.