Jump to content

drop dow list for date only inserts year into database


Lisa23

Recommended Posts

Hi i have a drop down menu for date which is meant to insert all 3 values into a date column bt is only sending the year how can i fix it

 

<select name="date_of_birth">

    <option value="1">January

    <option value="2">February

    <option value="3">March

    <option value="4">April

    <option value="5">May

    <option value="6">June

    <option value="7">July

    <option value="8">August

    <option value="9">September

    <option value="10">October

    <option value="11">November

    <option value="12">December

</select>

<select name="date_of_birth">

    <option value="1">1

    <option value="2">2

    <option value="3">3

    <option value="4">4

    <option value="5">5

    <option value="6">6

    <option value="7">7

    <option value="8">8

    <option value="9">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>

<select name="date_of_birth" id="year">

<?php

$year = date("Y");

for($i=$year;$i>$year-50;$i--)

{

if($year == $i)

echo "<option value='$i' selected>Current Year</option>";

else

echo "<option value='$i'>$i</option>";

}

?>

Link to comment
Share on other sites

yeah but then how do i sent into column because i have like this

 

mysql insert '$_POST[date_of_birth]' do i change that to what if all three will have different names??? i want all all to go into same column cz thers the month year and date id i give differnt name how do i make it all go into same column???

Link to comment
Share on other sites

You would concatenate the POSTed values in the query. For example, in your form:

<?php
$months = array('','January','February','March','April','May','June','July','August','September','October','November','December');
echo '<select name="month_of_birth">';
for ($i=1;$i<13;++$i) {
   echo '<option value="' . sprintf("%02d",$i) . '">' . $months[$i] . '</option>';
}
echo '</select>';
echo '<select name="day_of_birth">';
for ($i=1;$i<32;++$i) {
   echo '<option value="' . sprintf("%02d",$i) . '">' . $i . '</option>';
}
echo '</select>';
echo '<select name="year_of_birth">';
$year = date("Y");
for ($i = $year;$i > $year-50;$i--) {
   $s = ($i == $year)?' selected':'';
   echo '<option value="' . $i . '" ' . $s . '>' . $i . '</option>';
}
?>

Then in your processing script:

<?php
$date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth'];
$q = "insert into tbl set column_name = '$date_of_birth'";
?>

 

Note: syntax not checked for errors.

 

Ken

Link to comment
Share on other sites

sorry but my insert method is like this so i dnt really know how to implement urs into it??

 

mine

mysql_query("INSERT INTO driversnew (id, name, location, date_of_birth, car_number,
   favourite_track, least_favourite_track, achievements, sponsors, email, image, display)
VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$_POST[date_of_birth]','$_POST[date_of_month]','$_POST[date_of_year]','$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name','0')");
}

yours

<?php
$date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth'];
$q = "insert into tbl set column_name = '$date_of_birth'";
?>

Link to comment
Share on other sites

Mine was just an example, since I didn't know yours.

 

Here's yours with my example:

<?php
$date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth'];
$q = "INSERT INTO driversnew (id, name, location, date_of_birth, car_number,
   favourite_track, least_favourite_track, achievements, sponsors, email, image, display)
VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$date_of_birth','$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name','0')";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
?>

BTW, you really should be validating the input from your form, or you will run into problems.

 

Ken

Link to comment
Share on other sites

Like I said "syntax not checked for errors" -- I left out the "</select>" tag at the end of the last <select>.

 

When I have trouble getting data from form, I put

<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';
?>

at the start of the processing script. This will show the data that your form is sending. It can be very helpful.

 

Ken

Link to comment
Share on other sites

this come up bt still doenst sent date

Array

(

    [name] => phpfreaks

    [location] => uyfuy

    [month_of_birth] => 01

    [day_of_birth] => 01

    [year_of_birth] => 2010

    [car_number] => carnumber

    [favourite_track] => uyf

    [least_favourite_track] =>

    [achievements] =>

    [sponsors] =>

    => newyork@hotmail.com

    [verif_box] => 4342

    [submit] => Submit

)

 

Link to comment
Share on other sites

$q = "INSERT INTO driversnew (id, name, location, date_of_birth, car_number,

  favourite_track, least_favourite_track, achievements, sponsors, email, image, display)

VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$date_of_birth','$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST', '$image_name','0')";

$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());

 

and the form

<?php
$months = array('','January','February','March','April','May','June','July','August','September','October','November','December');
echo '<select name="month_of_birth">';
for ($i=1;$i<13;++$i) {
   echo '<option value="' . sprintf("%02d",$i) . '">' . $months[$i] . '</option>';
}
echo '</select>';
echo '<select name="day_of_birth">';
for ($i=1;$i<32;++$i) {
   echo '<option value="' . sprintf("%02d",$i) . '">' . $i . '</option>';
}
echo '</select>';
echo '<select name="year_of_birth">';
$year = date("Y");
for ($i = $year;$i > $year-50;$i--) {
   $s = ($i == $year)?' selected':'';
   echo '<option value="' . $i . '" ' . $s . '>' . $i . '</option>';
}
echo '</select>';
?>

Link to comment
Share on other sites

yeah i have that line this what comes up

$date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth'];

 

INSERT INTO driversnew (id, name, location, date_of_birth, car_number, favourite_track, least_favourite_track, achievements, sponsors, email, image, display) VALUES ('', 'tv', 'location','2010-01-01','carnumber','', '','', '','newyork@hotmail.com', '','0')

Link to comment
Share on other sites

No. The stripslashes function will only affect variable whose values contain a backslash.

 

It might be best to post what your code currently looks like. Also post the format of your database table, as there could be a problem with the datatype of the "date_of_birth" column.  I assumed it is a DATE type.

 

Ken

Link to comment
Share on other sites

ok is posting now but i have this echo back to display bck but it doesnt display back the date

 

    echo "<td>" . $_POST['date_of_birth'] . "</td>";

 

and also this one that send to email doesnt display the date on the email

 

$message = "
	Name:					$name  \n

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.