MySQL Forums
Forum List  »  PHP

Help inserting data into database from a <FORM>
Posted by: Anson Bolinger
Date: July 22, 2005 01:14PM

I'm learning PHP & MySQL but I seem to be stuck on this one thing.
I have figured out how to retrieve data from the database and display it in a PHP page. That was easy.

However, in a seperate PHP page, I have created a form to insert data into the database but I can't seem to get it to work.

The data in my database is rather simple. Just a band's tour info. dates, venues, showtimes, etc... I am including what I have done so far in hopes someone can tell me what I am doing wrong.

This is part my form. The part with the problem. It only has 3 select boxes. "month" "day" "year"

<FORM method="post" action="insert_event.php">
Date:&nbsp;
<select name="month">
<option selected>
<option value="01">January
<option value="02">February
<option value="03">March
<option value="04">April
<option value="05">May
<option value="06">June
<option value="07">July
<option value="08">August
<option value="09">September
<option value="10">October
<option value="11">November
<option value="12">December
</select>&nbsp;

<select name="day">
<option selected>
<option value="01">1
<option value="02">2
<option value="03">3
<option value="04">4
<option value="05">5
<option value="06">6
<option value="07">7
<option value="08">8
<option value="09">9
<option value="10">10
<option value="11">11
<option value="12">12
<option value="13">13
<option value="14">14
<option value="15">15
<option value="16">16
<option value="17">17
<option value="18">18
<option value="19">19
<option value="20">20
<option value="21">21
<option value="22">22
<option value="23">23
<option value="24">24
<option value="25">25
<option value="26">26
<option value="27">27
<option value="28">28
<option value="29">29
<option value="30">30
<option value="31">31
</select>&nbsp;

<select name="year">
<option selected value="2005">2005
<option value="2006">2006
<option value="2007">2007
<option value="2008">2008
<option value="2009">2009
<option value="2010">2010
</select>

<input type="submit" value="submit">
</FORM>

The problem is that I need all 3 of those values to be inserted into 1 column in the database named "date" as well as put in the default date format, 0000-00-00.

I can not figure out how to do this.
I keep getting, "Couldn't execute query." when I submit my form.

Here is my program for inserting the data. I know something is wrong with it, I just can't figure out what.

<?php
$date=$HTTP_POST_VARS['year-month-day'];
$start_time=$HTTP_POST_VARS['start_time'];
$end_time=$HTTP_POST_VARS['end_time'];
$event_name=$HTTP_POST_VARS['event_name'];
$event_site=$HTTP_POST_VARS['event_site'];
$venue_name=$HTTP_POST_VARS['venue_name'];
$venue_site=$HTTP_POST_VARS['venue_site'];
$address=$HTTP_POST_VARS['address'];
$city=$HTTP_POST_VARS['city'];
$state=$HTTP_POST_VARS['state'];
$zip=$HTTP_POST_VARS['zip'];
if (!$start_time || !$end_time)
{
echo 'You have not entered all the required details.<br />'
.'Please go back and try again.';
exit;
}
$date = addslashes($date);
$start_time = addslashes($start_time);
$end_time = addslashes($end_time);
$event_name = addslashes($event_name);
$event_site = addslashes($event_site);
$venue_name = addslashes($venue_name);
$venue_site = addslashes($venue_site);
$address = addslashes($address);
$city = addslashes($city);
$state = addslashes($state);
$zip = addslashes($zip);
include("rotwr_config.inc");
$connection = mysql_connect($hostname,$username,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
$query = "INSERT INTO calendar VALUES
('".$date."', '".$start_time."', '".$end_time."', '".$event_name."', '".$event_site."', '".$venue_name."', '".$venue_site."', '".$address."', '".$city."', '".$state."', '".$zip."')";
$result = mysql_query($query)
or die ("Couldn't execute query.");
if ($result)
echo mysql_affected_rows().' event added into calendar. ';
?>

Any help would be greatly appreciated. Thanks.

Options: ReplyQuote


Subject
Written By
Posted
Help inserting data into database from a <FORM>
July 22, 2005 01:14PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.