从数据库验证中选择列表

时间:2014-01-23 22:32:06

标签: php mysql sql validation

我将真诚地感谢您为解决此错误而付出的努力。 从数据库中做了一个选择(下拉列表)。然后下拉列表工作(因为它显示在 我是一个正常的形式。),我无法得到验证权。或者有些错误,我不知道。 如果我从列表中选择一个国家,它会在表格中给出错误(请选择业务总部国家)

这是表格:

    <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>"  />
Country of Residence of Business' Head Office<br />
<select name="country_location" class="select">
<option> Select Country</option>
<?php
include('inc/mysqli_connect.php');
$q ="SELECT id, CONCAT_WS('', country) FROM country ORDER BY country ASC";
$r =  mysqli_query($dbc, $q);
if(mysqli_num_rows($r) > 0){
while ($row = mysqli_fetch_array($r, MYSQLI_NUM)){
echo "<option value=\"$row[0]\"";
// check for stickyness
if(isset($_POST['country']) && ($_POST['country']==$row[0])) echo'country="country"';
echo ">$row[1]</option>\n";
}
}
mysqli_close($dbc);
 ?>
</select><br />
<input type="hidden" name="submitted" value="TRUE" /><br />
<input type="submit" name="submit" value="Register"  id="submit"/>
</div>
</form>

然后验证

  <?php
  require_once('inc/config_inc.php');
  $page_title = 'Register';

include('inc/session.html');
if(isset($_POST['submitted'])){
  require_once ('inc/mysqli_connect.php');
  $trimmed =array_map('trim', $_POST);
  $business_name=$country_location=$business_city=$nature_of_business=$email=$telephone=$director_last_name=$director_first_name=$password=FALSE;

if(isset($_POST['country_location']) && ($_POST['country_location'] == 'country') && ($_POST['country'] > 0)){
 $country_location = (int)$_POST['country'];
 }
 else{
 $country_location = FALSE; 
 echo'<p class="error">Please select business\' head office country!</p>';
  }
   if($business_name && $country_location && $business_city && $nature_of_business && $email 
   && $telephone && $director_last_name && $director_first_name && $password){
   "SELECT registration_id FROM users WHERE email='$email' ";
   $q = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br /> MYSQL Error:
   ".mysqli_error($dbc));
   if(mysqli_num_rows($r) == 0){
   $a = md5(uniqid(rand(), true));

   $q = "INSERT INTO registration( business_name,country_location,business_city,nature_of_business,email,telephone,director_last_name,director_first_name, ,password ,active,registration_date) 
                                VALUES
( '$business_name', '$country_location', '$business_city' ,'$nature_of_business', '$email', '$telephone', '$director_last_name','$director_first_name','$active','SHA1($password)' NOW())";

$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br \> MySQL Error:     ".mysqli_error($dbc));

if(mysqli_affected_rows($dbc) ==1){  // send email
$body =" Thank you for registering at....You are guaranteed customer statifaction!. To activate your account, please click on this lin:\n\n";
$body .=BASE_URL .'activate.php?x='.urlencode($email) . "&y=$a"; mail($trimmed
['email'], 'Registration Confirmation', $body, 'From: example.com');
echo'<h3 > Thank you for registering! A confirmation email has been sent to your email address. Click on the link in that email to activate your account.</h3>';

include('inc/footer.php');
exit();
}
else{ 
echo'<p class="error"> You could not be registered due to a system error. We apologise for any inconvinience.</p>';
}
}
else{ echo'<p class="error"> The email address entered has been registered.if you have forgotten your password, click link to <a href="recover_password.php">recover your password</a></p>';
}
} 
else{ echo'<p class = "error">Please re-enter your password and try again.</p>';
}mysqli_close($dbc);
}
  ?>

数据库:

数据库名称:用户 表名:国家。 行:id(int),值(国家/地区名称),国家/地区名称

请注意表格中有很多字段,我只指出了观察字段,因此可以看到验证。

请注意,我是一个绝对的新开发者,也是这个论坛。我可能 违反了规则。请纠正我,而不是侮辱,我会一直纠正。

提前感谢!

1 个答案:

答案 0 :(得分:0)

在表单中,您拥有<select name="country_location">,并将每个<option>的值设置为每个国家/地区的ID(echo "<option value=\"$row[0]\"";)。 这意味着,在过帐表单时,$_POST['country_location']的值是所选的国家/地区ID。

在验证中,您需要检查以下内容:

if(isset($_POST['country_location']) && ($_POST['country_location'] == 'country') && ($_POST['country'] > 0)){

$_POST['country_location']如何才能拥有“国家/地区”的价值?它应该保存国家的ID。 您尝试设置<option country="country">,但这不起作用。只有<option value>将通过POST发送。

如果您要将上述行更改为:

if(isset($_POST['country_location']) && ctype_digit($_POST['country_location']) && ($_POST['country'] > 0)){

ctype_digit检查值是否仅包含数字。有关详细信息,请参阅PHP: ctype_digit - Manual。由于我希望您的ID是数字的,这应该可以解决问题。