Jump to content


Photo

Inserting a Date from a html form


  • Please log in to reply
5 replies to this topic

#1 a2bardeals

a2bardeals
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationAnn Arbor, MI

Posted 13 June 2006 - 04:49 PM

I am having trouble setting up a HTML form to include an event in a event calander
i have 4 colums in a table

name, listing, event, date
(Name, Listing Link, Event Description, Date of Event)

all except the date are TEXT types the date is obviusly the DATE type but when i go to insert data into the date field it returns 0000-00-00 heres the insert code:

insert.html - the file with the form on it.
<html>
<head><title>Insert Test</title>
</head>
<body>
<form action="insert_db.php" method="POST">
Enter Bar Name: <input type="text" name="Name" />
Enter Listing Link: <input type="text" name="Listing" />
Enter Event: <input type="text" name="Event" />
Enter Date (YYYY-MM-DD): <input type="text" name="Date" />
<input type="submit" />
</form></body></html>

insert_db.php - the form action

<? 
$con = mysql_connect("localhost","NAME", "PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db_events", $con);

$sql="INSERT INTO events
(Name,Listing,Event,Date)
VALUES
('$_POST[Name]','$_POST[Listing]','$_POST[Event]', '$_POST[Date]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

echo "Success!";
?>


any ideas?

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 June 2006 - 05:25 PM

There's an inconsistency between your description and your code. The description says the name of the event date field is 'date', while the query uses the fieldname 'Date' (which is typical of all your names/Names). Are you sure you have the right names for all data entry and retrieval?
Legend has it that reading the manual never killed anyone.
My site

#3 a2bardeals

a2bardeals
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationAnn Arbor, MI

Posted 13 June 2006 - 06:57 PM

well i figured out the problem was a space before the $_POST[Date] however I have changed the code so that in the HTML form page there are 3 pull down menus named day, month, year.
In the insert_db.php action page i have used the following code:
[code=auto:0]
$y=$_POST[year];
$m=$_POST[month];
$d=$_POST[day];
$dash = '-';
$all = $y.$dash.$m.$dash.$d;

$sql="INSERT INTO events
(Name,Listing,Event,Date)
VALUES
('$_POST[Name]','$_POST[Listing]','$_POST[Event]',$all)";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

echo "Success!";
echo $all;

when echo is called it echos the right format for the date 2006-06-13 but it does not insert that into the database...

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 14 June 2006 - 06:29 AM

You still need to enclose your date variable in single quotes -- dates are string literals, like your other VALUEs.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 bolty2uk

bolty2uk
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 18 June 2006 - 07:01 AM

Hey administator try this...

Leave the date your user put in in a date field...

When the user inputs the form use simple drop down option boxes to choose the date..
Remember to put the correct values for each....( 1st, 2nd 3rd will be 01, 02, 03 and months will be the same jan , feb, march 01, 02, 03...) you get me..
This will save you much hassle as they will be entering a date that you know how to handle and is real..

Next put these in to named variables on the form, so day, month and year....

So you will end up with this..

$day="01"
$month="02"
$year="2006"

so the date the user entered for his event would be 1st february 2006..

Next step you will need to put these variables so mysql can take them into a date field...
So when you insert to mysql you will need this...

$name=$_POST['name'];
$date="{$_POST['year']}-{$_POST['month']}-{$_POST['day']}";
$event=$_POST['event'];
$listing=$_POST['listing'];

Now enter the values to mysql...

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]"INSERT INTO events VALUES ('$name','$date','$event','$listing')";[/quote]

Now the date we entered will sit in mysql DATE field like this 2006 02 01

Now obviously getting the date out like that is no good so to get it from that to 1st February 2006 you will need this....

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]mysql_query("SELECT name, DATE_FORMAT(date,'%D %M %Y') AS date, event, listing FROM events");[/quote]

Rock on tommy

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 18 June 2006 - 09:52 PM

Or, you could use a proper date picker so that Feburary 31st is never selected, and leap years work too... otherwise, you'll get garbage no matter what. But that's not really a DB issue.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users