Results 1 to 2 of 2

Thread: Php storing and updating birthdays

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Php storing and updating birthdays

    In a registration form (html) I have an input for a birthday. I'm not sure what format I should store it in the sql database.

    My sql dtabase looks like this

    PHP Code:
    CREATE TABLE users(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id),
    username VARCHAR(30),
    password VARCHAR(32),
    level int(4) default '1',
    suspended VARCHAR(5) default 'no',
    firstname VARCHAR(30),
    lastname VARCHAR(30),
    day VARCHAR(30),
    month VARCHAR(30),
    year VARCHAR(30),
    age VARCHAR(30),
    gender VARCHAR(30),
    email VARCHAR(30),
    actor VARCHAR(30)) 
    I need to be able to store their birthday, calculate their age (years) and update the age column to store the correct age once they have had a birthday.


    Here's all the relevant code


    My html form
    PHP Code:
    <h1>Register an Account </h1> 
    <font size="-5"><a href="../registerhelp" target="_blank">Why do I have to fill out this information?</a></font>
    <br /><br />
    <form action="check/checkedit.php" method="post" name="agreeform" >
      <div class="form_left">
    Username:
      </div>
      <div class="form_right">
    <input type="text" name="username" maxlength="60" />
      </div>
      <div class="clear"></div>
      <div class="form_left">
    Password:
      </div>
      <div class="form_right">
    <input type="password" name="pass" maxlength="20" />
      </div>
      <div class="clear"></div>
      <div class="form_left">
    Confirm Password:
      </div>
      <div class="form_right">
    <input type="password" name="pass2" maxlength="20" />
      </div>
      <div class="clear"></div>
      <div class="form_left">
    First name:
      </div>
      <div class="form_right">
    <input type="text" name="firstname" maxlength="15" />
      </div>
      <div class="clear"></div>
      <div class="form_left">
    Last name:
      </div>
      <div class="form_right">
    <input type="text" name="lastname" maxlength="20" />
      </div>
      <div class="clear"></div>
      <div class="form_left">

      Birthday:
      </div>
      <div class="form_right">
      <?php
    // DAY
    ?>
    <select name="day">
    <?php
    for ($i  1$i 32$i++) {
      echo 
    "  <option>$i</option>\n";
    }
    ?>
    </select>
      <?php
    // MONTH
    ?>
    <select name="month">
    <option value="January" >January</option>
    <option value="February" >February</option>
    <option value="March" >March</option>
    <option value="April" >April</option>
    <option value="May" >May</option>
    <option value="June" >June</option>
    <option value="July" >July</option>
    <option value="August" >August</option>
    <option value="September" >September</option>
    <option value="October" >October</option>
    <option value="November" >November</option>
    <option value="December" >December</option>
    </select>

      <?php
     
    // YEAR
     
    ?>
     <?php
    $maxyear 
    date("Y");
    $eighty "82";
    $minyear $maxyear-$eighty
    ?>

    <select name="year">
    <?php
    for ($f  $maxyear$f $minyear$f--) {
      echo 
    "  <option>$f</option>\n";
    }
    ?>
    </select>
      
      
      </div>
      <div class="clear"></div>
      <div class="form_left">
      
    Gender:
      </div>
      <div class="form_right">
    <input type="radio" name="gender" value="Male" id="Male">Male

    <input type="radio" name="gender" value="Female" id="Female">Female
      </div>
      <div class="clear"></div>
      <div class="form_left">
    E-mail:
      </div>
      <div class="form_right">
    <input type="text" name="email" maxlength="30" />
      </div>
      <div class="clear"></div>
      <div class="form_left">
    What type of work are you interested in?

      </div>
      <div class="form_right">

      <!--
    <input type="radio" name="shade" value="Actor" id="actor" onclick="chgType()">Actor 

    <input type="radio" name="shade" value="Backstage" id="backstage" onclick="chgType()">Backstage worker

    <input type="radio" name="shade" value="Both" id="both" onclick="chgType()">Both<br />
    -->

    <input type="radio" name="shade" value="Actor" id="actor">Acting

    <input type="radio" name="shade" value="Backstage" id="backstage">Backstage work

    <input type="radio" name="shade" value="Both" id="both">Both<br />

    <!--
    <select name="typeSelector" id="typeSelector" style="visibility: hidden">
    <option>Set mover</option>
    <option>Sound</option>
    </select>
    -->

      </div>
      <div class="clear"></div>
      <div class="form_left">&nbsp;</div>
      <div class="form_right">
    <input type="submit" name="submit" value="Send!" />
      </div>
      <div class="clear"></div>
    </form>
    The processing page

    PHP Code:
    <body>

    <?php
    require "../../database.php";


     
    // This code runs if the form has been submitted
     
    if (isset($_POST['submit'])) { 

     
    // This makes sure they did not leave any fields blank
     
    if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] | !$_POST['firstname'] | !$_POST['lastname'] |  !$_POST['day'] | !$_POST['month'] | !$_POST['year'] | !$_POST['gender'] |  !$_POST['email'] )  
         {

             die(
    'You did not complete all of the required fields <br /><br /> <a href=../registeredit.php>Back</a> ');
         }

     
    // checks if the username is in use
         
    if (!get_magic_quotes_gpc()) {
             
    $_POST['username'] = addslashes($_POST['username']);
         }
     
    $usercheck $_POST['username'];
     
    $check mysql_query("SELECT username FROM users WHERE username = '$usercheck'"
    or die(
    mysql_error());
     
    $check2 mysql_num_rows($check);

     
    //if the name exists it gives an error
     
    if ($check2 != 0) {
             die(
    'Sorry, the username '.$_POST['username'].' is already in use. <br /><br /> <a href=../registereit.php>Back</a>');
                     }

     
    // this makes sure both passwords entered match
         
    if ($_POST['pass'] != $_POST['pass2']) {
             die(
    'Your passwords did not match. <br /><br /> <a href=../>Back</a> ');
         }

         
    // here we encrypt the password and add slashes if needed
         
    $_POST['pass'] = md5($_POST['pass']);
         if (!
    get_magic_quotes_gpc()) {
             
    $_POST['pass'] = addslashes($_POST['pass']);
             
    $_POST['username'] = addslashes($_POST['username']);
                 }
    if(
    $_POST[firstname] != '' && $_POST[firstname] != '' && $_POST[firstname] != '' && $_POST[firstname] != '') {
     
    // now we insert it into the database
     /*
     //Check if typeSelector is set.
    if($_POST[shade] == 'Backstage' || $_POST[shade] == 'Both') {  

    $type = $_POST[typeSelector];
    } else {
    $type = $_POST[shade];
    }  
    */
    $type $_POST[shade];

    // CALCULATING AND STORING THEIR AGE

    $day $_POST[day];
    $month $_POST[month];
    $year $_POST[year];


     
     
         
    $insert "INSERT INTO users (username, password, firstname, lastname, day, month, year, gender, email, actor)
                 VALUES ('"
    .$_POST['username']."', '".$_POST['pass']."', '$_POST[firstname]', '$_POST[lastname]', '$day', '$month', '$year', '$_POST[gender]', '$_POST[email]', '$type')";
         
    $add_member mysql_query($insert);

         echo <<<EOF
         <h1>Registered</h1>
     <p>Thank you, you have registered - you may now login</a>.</p>
    EOF;

         } else {
             echo 
    "Please fill in all of the required fields.";
         } 

     } 
     else 
     {    
     
    ?>


     <?php
     
    }
     
    ?>

    This is the bit

    PHP Code:
    // CALCULATING AND STORING THEIR AGE

    $day $_POST[day];
    $month $_POST[month];
    $year $_POST[year]; 

    Any help would be great!

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •