Jump to content

PHP is eating my numbers!


flyersun

Recommended Posts

I'm a bit of a newbie I'm doing a web design degree at the moment as as part of it we have to do php anyways I've created a script which inputs info into a mysql database.

Some of the info which needs to be inputted is numeric but the script seems to be eating some of the numbers before they even get to the database. It seems to like 1 and 0's the most. Any ideas how I can fix it's it's really a pain?


Amy
Link to comment
Share on other sites

<form method="post" action="customer_details_validate.php">
  <?php
  echo"<select name='days'>";
  $days = 1;
  while ($days <= 31) {
    echo"<option value='$days'>" . $days ."</option>";
$days++;
}
  echo"<select name='month'>";
  $month = 1;
  while ($month <= 12) {
    echo"<option value='$month'>" . $month ."</option>";
$month++;
}
echo "</select>";
  echo"<select name='year'>";
  $year = 2007;
  while ($year >= 1907) {
    echo"<option value='$year'>" . $year ."</option>";
$year--;
}
echo "</select>";
  ?>

<input type="submit" value="Submit">
 

This is my script to collect someone's date of  birth! it sends the information to another script which is then suposted to add the information to a database. However when the DOB reaches the next script some of the numbers have been chopped off.

Is that more clear?

I could post the whole script but it is rather large.

Any help you have give will be greatly appreciated.
Link to comment
Share on other sites

I haven't finished the script yet becuse I noticed the problem before it was finished but this is the script I'm using to text.


//Validate DOB
echo $formVars["year"] . $formVars["month"] . $formVars["days"];  [b]<----- this echo statment only gives me 3 or 4 numbers when it should give me 8.[/b]

  if ((empty($formVars["year"])) || (empty($formVars["month"])) || (empty($formVars["days"]))) {
 
//Users DOB cannot be a null string (empty)

$errorString .= "\n<br>You must supply a Date of Birth.";

}else{
$dob = $formVars["year"] . "--" . $formVars["month"] . "--" . $formVars["days"];

}

Link to comment
Share on other sites

try putting this right below //Validate DOB
[code]<?php //don't put the opening and closing php tags, they are there to highlight the code in this box
echo $_POST['month'] . "<br />";
echo $_POST['days'] . "<br />";
echo $_POST['year'];
?>[/code]

and see what you get
Link to comment
Share on other sites

do print_r($_POST) to make sure all of your post vars are named the way you think they should be.

PS: Instead of while loops, for loops would be more appropriate here.
for($i=1; $i<=31; $i++){
  echo '<option value="'.$i.'">'.$i.'</option>';
}
Link to comment
Share on other sites

Well I'm using echo not print but yep pretty much I will show you want I get:
Array ( [surname] => [firstname] => [address] => [town] => [county] => [postcode] => [days] => 1 [month] => 1 [year] => 1998 [email] => [tel] => [username] => [password] => [password2] => )

998 <-- this is all I get... hmm

$formVars[$varname] = trim($value, 100); <--- I do have this line in the script but I don't think that could be it ?

Link to comment
Share on other sites

you're not losing the zeroes, you never had them to start with:
[code]while ($days <= 31) {
      echo"<option value='$days'>" . $days ."</option>";
      $days++;
}[/code]
--when the $days variable has the value '5', no zero will be printed.  The code produced will read:
[code]<option value='5'>5</option>[/code]
--if you want the date to print with leading zeroes when necessary do this:
[code]// converts single digit number to 2 digit string with leading zeros
// single quotes around zero '0' forces conversion to string.
if ($formVars["month"] < 10) $formVars["month"] = '0' . $formVars["month"];
if ($formVars["days"] < 10) $formVars["days"] = '0' . $formVars["days"];
//will now always print 2 digits for month and year
echo $formVars["year"] . $formVars["month"] . $formVars["days"]; [/code]
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.