Jump to content

Archived

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

a2bardeals

Inserting a Date from a html form

Recommended Posts

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.
[code]
<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>
[/code]

insert_db.php - the form action

[code]
<?
$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!";
?>[/code]


any ideas?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]
$y=$_POST[year];
$m=$_POST[month];
$d=$_POST[day];
$dash = '-';
[b]$all = $y.$dash.$m.$dash.$d;[/b]

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

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

Share this post


Link to post
Share on other sites
You still need to enclose your date variable in single quotes -- dates are string literals, like your other VALUEs.

Share this post


Link to post
Share on other sites
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...

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

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 [b]2006 02 01[/b]

Now obviously getting the date out like that is no good so to get it from that to [b]1st February 2006[/b] 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.