Jump to content

Default value for DATE field?


pappakaka

Recommended Posts

Ok, i can't understand whats wrong with the DATE field in MySQL and PHP. I have a form in PHP witch has 3 birth day dropdown menus that looks like this (YYYY-MM-DD). An in my database i have a birth_day colum with DATE as type and i've tried to set the default value to "None" and "0000-00-00" but nothing works. Everytime i try to input something (e.g. 1993-16-05) i get this error:

 

Incorrect date value: '05' for column 'birth_day' at row 1

 

I've tried to set the value for the "Day" dropdown in the PHP form to both 5 and 05 but still nothing, what am i doing wrong?

Link to comment
Share on other sites

Oh i'm sorry, my example was meant to be like this (e.g. 1993-12-05)

 

But i tried it and still the same result, but it seems to be the last dropdown that doesn't work! even if i have the "Year", "Day" or "Month" dropdown at the end, it's only the last dropdown i get an error from not the other two no mather what value i put into them. This is so wierd, i've tried alot of things now and nothing seems to make a difference to the problem, it's always the same so i can't rule out whats wrong..

Link to comment
Share on other sites

Yes ofc, here is the html form:

				<li>
				<label for="birth_day">* Birth Day: </label><br />
				<select name="birth_day" required="required">
					<option disabled="disabled" selected="selected">Year</option>
					<option value="2011">2011</option>
					<option value="2010">2010</option>
					<option value="2009">2009</option>
					<option value="2008">2008</option>
					<option value="2007">2007</option>
					<option value="2006">2006</option>
					<option value="2005">2005</option>
					<option value="2004">2004</option>
                                                AND SO ON....
				</select>
				<select name="birth_day" required="required">
					<option disabled="disabled" selected="selected">Day</option>
					<option value="01">1</option>
					<option value="02">2</option>
					<option value="03">3</option>
					<option value="04">4</option>
					<option value="05">5</option>
					<option value="06">6</option>
					<option value="07">7</option>
					<option value="08">8</option>
                                                AND SO ON.....
				</select>
				<select name="birth_day" required="required">
					<option disabled="disabled" selected="selected">Month</option>
					<option value="01">January</option>
					<option value="02">February</option>
					<option value="03">March</option>
					<option value="04">April</option>
					<option value="05">May</option>
					<option value="06">June</option>
					<option value="07">July</option>
					<option value="08">August</option>
					<option value="09">September</option>
					<option value="10">October</option>
					<option value="11">November</option>
					<option value="12">December</option>
				</select>
			</li>

 

And here is the whole php code:

 

<?php

class Register
{
  private $username;
  private $first_name;
  private $last_name;
  private $password;
  private $passmd5;
  private $email;
  private $gender;
  private $birth_day;

  private $errors;
  private $token;

  public function __construct()
  {
    $this->errors = array();

    $this->username    = $this->filter($_POST['username']);
$this->first_name  = $this->filter($_POST['first_name']);
$this->last_name   = $this->filter($_POST['last_name']);
    $this->password    = $this->filter($_POST['password']);
    $this->email       = $this->filter($_POST['email']);
$this->gender      = $this->filter($_POST['gender']);
$this->birth_day   = $this->filter($_POST['birth_day']);
    $this->token       = $_POST['token'];

    $this->passmd5    = md5($this->password);
  }

  public function process()
  {
    if($this->valid_token() && $this->valid_data())
         $this->register();

    return count($this->errors)? 0 : 1;
  }

  public function filter($var)
  {
    return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
  }

  public function register()
  {
   mysql_connect("localhost","root","") or die(mysql_error());
   mysql_select_db("membership") or die (mysql_error());

   $sql = "INSERT INTO users(username,password,first_name,last_name,email,gender,birth_day) VALUES ('{$this->username}','{$this->passmd5}','{$this->first_name}','{$this->last_name}','{$this->email}','{$this->gender}','{$this->birth_day}')";
mysql_query($sql) or die(mysql_error());

   if(mysql_affected_rows()< 1)
     $this->errors[] = 'Could Not Process Form';
  }

  public function user_exists()
  {
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("membership") or die (mysql_error());

    $data = mysql_query("SELECT username FROM users WHERE username = '{$this->username}'");

    return mysql_num_rows($data) > 0 ? 1 : 0;
  }

  public function show_errors()
  {
    echo "<h3>Errors</h3>";

    foreach($this->errors as $key=>$value)
      echo $value."<br>";
  }

  public function valid_data()
  {
    if($this->user_exists())
      $this->errors[] = 'Username already taken, choose another one!';
    if(empty($this->username))
      $this->errors[] = 'Please enter a valid username!';
if(empty($this->first_name))
      $this->errors[] = 'Please enter a valid first name!';
if(empty($this->last_name))
      $this->errors[] = 'Please enter a valid last name!';
    if(empty($this->password))
      $this->errors[] = 'Please enter a valid password!';
    if(empty($this->email) || !preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/',$this->email))
      $this->errors[] = 'Please enter a valid email address!';
if(empty($this->gender))
      $this->errors[] = 'Please fill in your gender!';
if(empty($this->birth_day))
      $this->errors[] = 'Please fill in your birth day!';

  return count($this->errors)? 0 : 1;
  }


  public function valid_token()
  {
   if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
     $this->errors[] = 'Invalid Submission';

   return count($this->errors)? 0 : 1;
  }
}

?>

Link to comment
Share on other sites

Oh but why doesn't it take the first of the <select>s then? shouldn't it start with the first one? and how could i make them all be sent away as one single date! otherwise i would have to set one name= attribute for each dropdown. Like "birth_day""birth_year" "birth_month"?

Link to comment
Share on other sites

Frankly, I'd probably use a javascript date picker, but for the <noscript> action I'd make them an array, and name them dob['y'], dob['m'], and dob['d'], then validate and concatenate the values for the query string. Let me know if you need a hand with that part of it.

Link to comment
Share on other sites

correct: i would have to set one name= attribute for each dropdown. Like "birth_day""birth_year" "birth_month"?

 

Ok thank you!

 

Frankly, I'd probably use a javascript date picker, but for the <noscript> action I'd make them an array, and name them dob['y'], dob['m'], and dob['d'], then validate and concatenate the values for the query string. Let me know if you need a hand with that part of it.

 

I would have used JS if it wasn't for one reason.. many people (including people i know) browse websites with JS turned "OFF" so i'm trying to keep the JS on my site to a minimum for the important feutures like login, register and menus etc. so that people still can use my website without it. But thank you anyway!

Link to comment
Share on other sites

One more quick question, what do these lines mean:

 

Notice: Undefined index: birth_month in [link to php file] on line 29

Notice: Undefined index: birth_day in [link to php file] on line 30

Notice: Undefined index: birth_years in [link to php file] on line 31

 

They are already defined like this..

 

  private $birth_month;

  private $birth_day;

  private $birth_year;

 

..before line 29, 30 and 31. An if it means in MySQL i've already created 3 columns to hold the information?  :confused:

Link to comment
Share on other sites

It means you're attempting to access those values, but they're undefined. Probably due to not checking if the form is submitted, and their values are set first. That should be part of your validation routine, to make sure the values are present and within range.

Link to comment
Share on other sites

But that doesn't make sense cause the gender dropdown is the same as the date ones and that works just fine? See here, i've marked the important lines:

 

<li>
				<label for="gender">* Gender: </label><br />
				[b]<select name="gender" value="gender" required="required">[/b]
					<option>Man</option>
					<option>Woman</option>
				</select>
			</li>
<li>
				<label>* Birth Day: </label><br />
				--------<select name="birth_month" value="birth_month" required="required">-------------
					<option disabled="disabled" selected="selected">Day</option>
					<option value="01">1</option>
					<option value="02">2</option>
					<option value="03">3</option>
					<option value="04">4</option>
					<option value="05">5</option>
				</select>
				--------------<select name="birth_day" value="birth_day" required="required">-------------
					<option disabled="disabled" selected="selected">Month</option>
					<option value="01">January</option>
					<option value="02">February</option>
					<option value="03">March</option>
					<option value="04">April</option>
					<option value="05">May</option>
					<option value="06">June</option>
					<option value="07">July</option>
					<option value="08">August</option>
					<option value="09">September</option>
					<option value="10">October</option>
					<option value="11">November</option>
					<option value="12">December</option>
				</select>
				-------<select name="birth_year" value="birth_year" required="required">------------
					<option disabled="disabled" selected="selected">Year</option>
					<option value="2011">2011</option>
					<option value="2010">2010</option>
					<option value="2009">2009</option>
					<option value="2008">2008</option>
					<option value="2007">2007</option>
					<option value="2006">2006</option>
					<option value="2005">2005</option>
				</select>
			</li>

 

And here is the PHP code again:

<?php

class Register
{
  private $username;
  private $first_name;
  private $last_name;
  private $password;
  private $passmd5;
  private $email;
  private $gender;
  private $birth_month;
  private $birth_day;
  private $birth_year;

  private $errors;
  private $token;

  public function __construct()
  {
    $this->errors = array();

    $this->username     = $this->filter($_POST['username']);
$this->first_name   = $this->filter($_POST['first_name']);
$this->last_name    = $this->filter($_POST['last_name']);
    $this->password     = $this->filter($_POST['password']);
    $this->email        = $this->filter($_POST['email']);
$this->gender       = $this->filter($_POST['gender']);
$this->birth_month  = $this->filter($_POST['birth_month']);
$this->birth_day    = $this->filter($_POST['birth_day']);
$this->birth_year   = $this->filter($_POST['birth_year']);
    $this->token        = $_POST['token'];

    $this->passmd5      = md5($this->password);
  }

  public function process()
  {
    if($this->valid_token() && $this->valid_data())
         $this->register();

    return count($this->errors)? 0 : 1;
  }

  public function filter($var)
  {
    return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
  }

  public function register()
  {
   mysql_connect("localhost","root","") or die(mysql_error());
   mysql_select_db("membership") or die (mysql_error());

   $sql = "INSERT INTO users(username,password,first_name,last_name,email,gender,birth_month,birth_day,birth_year) VALUES ('{$this->username}','{$this->passmd5}','{$this->first_name}','{$this->last_name}','{$this->email}','{$this->gender}','{$this->birth_month}','{$this->birth_day}','{$this->birth_year}')";
mysql_query($sql) or die(mysql_error());

   if(mysql_affected_rows()< 1)
     $this->errors[] = 'Could Not Process Form';
  }

  public function user_exists()
  {
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("membership") or die (mysql_error());

    $data = mysql_query("SELECT username FROM users WHERE username = '{$this->username}'");

    return mysql_num_rows($data) > 0 ? 1 : 0;
  }

  public function show_errors()
  {

    foreach($this->errors as $key=>$value)
      echo "<div class=errormessages> 
				$value
		</div>
		<br />";
  }

  public function valid_data()
  {
    if($this->user_exists())
      $this->errors[] = 'The username is already taken, choose another one!';
    if(empty($this->username))
      $this->errors[] = 'You must enter a username!';
if(empty($this->first_name))
      $this->errors[] = 'You must enter your first name';
    if(empty($this->password))
      $this->errors[] = 'You must enter a valid password!';
    if(empty($this->email) || !preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/',$this->email))
      $this->errors[] = 'You must enter an email address!';
if(empty($this->gender))
      $this->errors[] = 'Select your gender!';
if(empty($this->birth_month))
      $this->errors[] = 'Select which month you were born!';
if(empty($this->birth_day))
      $this->errors[] = 'Select which day you were born!';
if(empty($this->birth_year))
      $this->errors[] = 'Select which year you were born!';

  return count($this->errors)? 0 : 1;
  }


  public function valid_token()
  {
   if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
     $this->errors[] = 'Invalid Submission';

   return count($this->errors)? 0 : 1;
  }
}

?>

 

Link to comment
Share on other sites

well, that isn't the whole form, just the part where the problem is about. If it helps you help me, here is the whole HTML form too:

<div class="register">
	<header>Sign Up Now!</header>
	<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
		<ul>
			<li>
				<label for="username">* Username: </label><br />
				<input name="username" autofocus="on" class="rusernamefield" type="text" required="required"></input>
			</li>
			<li>
				<label for="first_name">* First Name: </label><br />
				<input name="first_name" class="rfirstnamefield" type="text" required="required"></input>
			</li>
			<li>
				<label for="last_name">Last Name: </label><br />
				<input name="last_name" class="rlastnamefield" type="text"></input>
			</li>
			<li>
				<label for="password">* Password: </label><br />
				<input name="password" class="rpasswordfield" onkeyup='password_strength(this.value)' minlength="6" maxlength="30" type="password" required="required"></input>
			</li>

			<div id="password_strength_border">
				<div id="password_strength" class="strength0"></div>
			</div>

			<li>
				<label for="email">* Email Address: </label><br />
				<input name="email" class="remail" type="email" required="required" placeholder="email@address.com"></input>
			</li>
			<li>
				<label for="confemail">* Confirm Email Address: </label><br />
				<input name="confemail" class="rconfirmemail" type="email" required="required" placeholder="email@address.com"></input>
			</li>
			<li>
				<label for="gender">* Gender: </label><br />
				<select name="gender" value="gender" required="required">
					<option>Man</option>
					<option>Woman</option>
				</select>
			</li>
			<li>
				<label for="birth_month">* Birth Day: </label><br />
				<select name="birth_month" value="birth_month" required="required">
					<option disabled="disabled" selected="selected">Day</option>
					<option value="01">1</option>
					<option value="02">2</option>
					<option value="03">3</option>
					<option value="04">4</option>
					<option value="05">5</option>
					<option value="06">6</option>
					<option value="07">7</option>
					<option value="08">8</option>
					<option value="09">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="birth_day" value="birth_day" required="required">
					<option disabled="disabled" selected="selected">Month</option>
					<option value="01">January</option>
					<option value="02">February</option>
					<option value="03">March</option>
					<option value="04">April</option>
					<option value="05">May</option>
					<option value="06">June</option>
					<option value="07">July</option>
					<option value="08">August</option>
					<option value="09">September</option>
					<option value="10">October</option>
					<option value="11">November</option>
					<option value="12">December</option>
				</select>
				<select name="birth_year" value="birth_year" required="required">
					<option disabled="disabled" selected="selected">Year</option>
					<option value="2011">2011</option>
					<option value="2010">2010</option>
					<option value="2009">2009</option>
					<option value="2008">2008</option>
					<option value="2007">2007</option>
					<option value="2006">2006</option>
					<option value="2005">2005</option>
					<option value="2004">2004</option>
					<option value="2003">2003</option>
					<option value="2002">2002</option>
					<option value="2001">2001</option>
					<option value="2000">2000</option>
					<option value="1999">1999</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>
					<option value="1989">1989</option>
					<option value="1988">1988</option>
					<option value="1987">1987</option>
					<option value="1986">1986</option>
					<option value="1985">1985</option>
					<option value="1984">1984</option>
					<option value="1983">1983</option>
					<option value="1982">1982</option>
					<option value="1981">1981</option>
					<option value="1980">1980</option>
					<option value="1979">1979</option>
					<option value="1978">1978</option>
					<option value="1977">1977</option>
					<option value="1976">1976</option>
					<option value="1975">1975</option>
					<option value="1974">1974</option>
					<option value="1973">1973</option>
					<option value="1972">1972</option>
					<option value="1971">1971</option>
					<option value="1970">1970</option>
					<option value="1969">1969</option>
					<option value="1968">1968</option>
					<option value="1967">1967</option>
					<option value="1966">1966</option>
					<option value="1965">1965</option>
					<option value="1964">1964</option>
					<option value="1963">1963</option>
					<option value="1962">1962</option>
					<option value="1961">1961</option>
					<option value="1960">1960</option>
					<option value="1959">1959</option>
					<option value="1958">1958</option>
					<option value="1957">1957</option>
					<option value="1956">1956</option>
					<option value="1955">1955</option>
					<option value="1954">1954</option>
					<option value="1953">1953</option>
					<option value="1952">1952</option>
					<option value="1951">1951</option>
					<option value="1950">1950</option>
					<option value="1949">1949</option>
					<option value="1948">1948</option>
					<option value="1947">1947</option>
					<option value="1946">1946</option>
					<option value="1945">1945</option>
					<option value="1944">1944</option>
					<option value="1943">1943</option>
					<option value="1942">1942</option>
					<option value="1941">1941</option>
					<option value="1940">1940</option>
					<option value="1939">1939</option>
					<option value="1938">1938</option>
					<option value="1937">1937</option>
					<option value="1936">1936</option>
					<option value="1935">1935</option>
					<option value="1934">1934</option>
					<option value="1933">1933</option>
					<option value="1932">1932</option>
					<option value="1931">1931</option>
					<option value="1930">1930</option>
					<option value="1929">1929</option>
					<option value="1928">1928</option>
					<option value="1927">1927</option>
					<option value="1926">1926</option>
					<option value="1925">1925</option>
					<option value="1924">1924</option>
					<option value="1923">1923</option>
					<option value="1922">1922</option>
					<option value="1921">1921</option>
					<option value="1920">1920</option>
					<option value="1919">1919</option>
					<option value="1918">1918</option>
					<option value="1917">1917</option>
					<option value="1916">1916</option>
					<option value="1915">1915</option>
					<option value="1914">1914</option>
					<option value="1913">1913</option>
					<option value="1912">1912</option>
					<option value="1911">1911</option>
					<option value="1910">1910</option>
					<option value="1909">1909</option>
					<option value="1908">1908</option>
					<option value="1907">1907</option>
					<option value="1906">1906</option>
					<option value="1905">1905</option>
					<option value="1904">1904</option>
					<option value="1903">1903</option>
					<option value="1902">1902</option>
					<option value="1901">1901</option>
					<option value="1900">1900</option>
				</select>
			</li>
			<li>
				<label for="iagree" class="iagreetext">*  I Agree to the <a href="#">Privacy Policy</a> and <a href="#">Terms of Use</a></label>
				<input name="iagree" type="checkbox" required="required" class="iagreebox"></input>
			</li>
			<input name="register" class="registerbutton" type="submit" value="Sign Up"></input>
			<p class="fieldsmarked">Fields marked with an (*) is required</p>
			<input type="hidden" name="token" value="<?php echo $token;?>"/>
		</ul>
	</form>
	</div>

 

Link to comment
Share on other sites

 <select name="gender" value="gender" required="required">
                  <option>Man</option>
                  <option>Woman</option>
               </select>

 

Is incorrect. Selects do not have a value attribute. The option should have a selected attribute.

<option selected="selected">Man</option

 

Apply to all your selects.

Link to comment
Share on other sites

Ok, I solved it! for those who maybe have a similar problem here is what fixed it for me..

 

I changed this:

	$this->birth_month  = $this->filter($_POST['birth_month']);
$this->birth_day    = $this->filter($_POST['birth_day']);
$this->birth_year   = $this->filter($_POST['birth_year']);

 

Into this:

	$this->birth_month  = $this->filter($_POST['birth_month'] = "");
$this->birth_day    = $this->filter($_POST['birth_day'] = "");
$this->birth_year   = $this->filter($_POST['birth_year'] = "");

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.