Certain field wont insert into the MySQL

Discussion in 'PHP' started by CuBz, Aug 13, 2010.

  1. #1
    I have date of birth on my register form. The day, month, and year are seperate fields on MySQL. So I have used the select input for visitors to choose their D.O.B but it only enters the month and year into the users table, the day stays at 0.

    The fields for the days are all numbers and are INT(11)

    Here is the HTML for the D.O.B (I wont paste all the years onto here as it will take up too much room)
    Date of Birth:
    <select name="month" class="input">
    	<option value="0">Day</option>
    	<option value="1">1</option>
    	<option value="2">2</option>
    	<option value="3">3</option>
    	<option value="4">4</option>
    	<option value="5">5</option>
    	<option value="6">6</option>
    	<option value="7">7</option>
    	<option value="8">8</option>
    	<option value="9">9</option>
    	<option value="10">10</option>
    	<option value="11">11</option>
    	<option value="12">12</option>
    	<option value="13">13</option>
    	<option value="14">14</option>
    	<option value="15">15</option>
    	<option value="16">16</option>
    	<option value="17">17</option>
    	<option value="18">18</option>
    	<option value="19">19</option>
    	<option value="20">20</option>
    	<option value="21">21</option>
    	<option value="22">22</option>
    	<option value="23">23</option>
    	<option value="24">24</option>
    	<option value="25">25</option>
    	<option value="26">26</option>
    	<option value="27">27</option>
    	<option value="28">28</option>
    	<option value="29">29</option>
    	<option value="30">30</option>
    	<option value="31">31</option>
    </select>
    
    <select name="month" class="input">
    	<option value="0">Month</option>
    	<option value="1">January</option>
    	<option value="2">Febuary</option>
    	<option value="3">March</option>
    	<option value="4">April</option>
    	<option value="5">May</option>
    	<option value="6">June</option>
    	<option value="7">July</option>
    	<option value="8">August</option>
    	<option value="9">September</option>
    	<option value="10">October</option>
    	<option value="11">November</option>
    	<option value="12">December</option>
    </select>
    
    <select name="year" class="input">
    	<option value="0">Year</option>
    	<option value="1998">1998</option>
    	<option value="1997">1997</option>
    	<option value="1996">1996</option>
    	<option value="1995">1995</option>
    	<option value="1994">1994</option>
    	<option value="1993">1993</option>
    	<option value="1992">1992</option>
    	<option value="1991">1991</option>
    	<option value="1990">1990</option>
    </select>
    HTML:

    Here is the php bits that are used for D.O.B (Once again i've not added the things on this forum that don't need to be shown)
    $day=$_POST['day'];
    $month=$_POST['month'];
    $year=$_POST['year'];
    
    if($day == "0"){
    die("You must select your Date of Birth");
    }if($month == "0"){
    die("You must select your Date of Birth");
    }if($year == "0"){
    die("You must select your Date of Birth");
    }
    
    mysql_query("INSERT INTO `users` ( `id` , `username` , `password` , `day` , `month` , `year` )
    VALUES (
    '', '$reg_username', '$reg_password', '$day', '$month', '$year'
    )");
    PHP:

     
    CuBz, Aug 13, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    In your HTML:

    You have 2 month select tags, theirfore change the first:

    
    <select name="month" class="input">
        <option value="0">Day</option>
    
    HTML:
    to:

    
    <select name="day" class="input">
        <option value="0">Day</option>
    
    HTML:
    Also replace your PHP with:

    //prevent sql parsing issues aswell as sql injection...
    $day   = mysql_real_escape_string($_POST['day']);
    $month = mysql_real_escape_string($_POST['month']);
    $year  = mysql_real_escape_string($_POST['year']);
    
    //simplified...
    if ($day == 0 || $month == 0 || $year == 0) {
        die('You must select your Date of Birth');
    }
    
    mysql_query("INSERT INTO users ('username', 'password', 'day', 'month', 'year')
    VALUES ('{$reg_username}', '{$reg_password}', '{$day}', '{$month}', '{$year}')") or trigger_error(mysql_error(), E_USER_ERROR);
    PHP:
    Let me know how that goes, also report back quoting any errors that may be apparent (if any)
     
    Last edited: Aug 13, 2010
    danx10, Aug 13, 2010 IP
  3. vinoth.t

    vinoth.t Peon

    Messages:
    156
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Just insert with raw query to conform is ur query is right or not
    INSERT INTO `users` ( `id` , `username` , `password` , `day` , `month` , `year` ) VALUES ('', 'test', 'test', '1', 'May', '2010')
     
    vinoth.t, Aug 13, 2010 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    you have duplicate month, and forgot to add day on your form...
     
    gapz101, Aug 13, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    good catch
     
    danx10, Aug 13, 2010 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Instead of escaping the dates, I'd rather (int)'em.

    
    $year = (int) $_POST['year'];
    $month = (int) $_POST['month'];
    $day = (int) $_POST['day'];
    
    PHP:
    And to push date verification a little further, I suggest:
    
    if (!checkdate($month, $day, $year))
        die('Invalid date');
    
    PHP:
    On a last note, I'm not sure what you're planning to do with this info, but is there a specific reason you're storing the birthday like this? Wouldn't it be better to keep it all in one field?
     
    nico_swd, Aug 13, 2010 IP
  7. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7

    Thanks, this worked.

    It was 'day' before, but when i copied the <Select> from month (because even before that it was not workin) i forgot to change the name. but its working now so thanks and thanks everyone else for your replies
     
    CuBz, Aug 13, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    No problem, but I don't deserve the credit so props to gapz101, as I didn't catch that, until gapz101 mentioned it.

    You should also consider taking alook at nico_swd's post on validation :)
     
    danx10, Aug 13, 2010 IP
  9. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Sorry I didn't see this before.
    Thanks I will try that later on.

    I was going to put them all in one field but I didn't know how to do it so it would update to one field, and also I didn't know how to do it so it would display as a date and display their age
     
    CuBz, Aug 13, 2010 IP
  10. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #10
    Might want to consider storing the year, month, and day as one column entry with a DATE or TIMESTAMP type. This would prevent the need for three seperate columns and would simply all the processing.
     
    Thorlax402, Aug 13, 2010 IP