【问题标题】:select list from database validation从数据库验证中选择列表
【发布时间】:2014-03-21 13:03:04
【问题描述】:

衷心感谢您为解决此错误所做的努力。 从数据库中做了一个选择(下拉列表)。当下拉列表工作时(因为它显示在 我是一个正常的形式。),我无法正确验证。或者我不知道的一些错误。 如果我从列表中选择一个国家,它会在表格中给出错误(请选择业务总部国家)

这是表格:

    <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);
}
  ?>

数据库:

数据库名称:用户 表名:国家。 rows:id (int), value(国名), country(国名)

请注意,表单中有很多字段,我只指出了 concerend 字段,因此看到了验证。

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

提前致谢!

【问题讨论】:

  • 您认为在选项标签中打印 country="country" 究竟会做什么?因为无论你认为它会做什么,它都不会那样做。
  • @developerwjk,我试图使该字段具有粘性,这样如果用户选择一个国家并发生错误,他将不会在提交第二个时间之前再次选择它。这是哪里的问题?我没有你在代码上工作一点。感谢您的回复。
  • 应该选择='true'

标签: php mysql sql validation


【解决方案1】:

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

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

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

但是$_POST['country_location'] 怎么会有值'country'?它应该包含该国家/地区的ID。 您尝试设置&lt;option country="country"&gt;,但这不起作用。只有&lt;option value&gt; 会通过 POST 发送。

如果您将上述行更改为:

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

ctype_digit 检查一个值是否仅由数字组成。请参阅PHP: ctype_digit - Manual 了解更多信息。因为我希望您的 ID 是数字,所以这应该可以解决问题。

【讨论】:

  • @DickW,非常感谢您的分析。你永远是我在这个行业的导师。太感谢了。该脚本现在似乎正在为小错误减少工作(不是发布的 1)
  • @user2989439 完全没问题 :-) 如果此答案解决了您的问题,请将其标记为已接受的答案!
  • @DickW。再次感谢,我已经完成了你的要求。但是我如何查询数据库以输出(打印)国家名称,因为它们的整数值(1、2、3 等)已记录。请帮助,因为这是我接下来需要工作的领域。感谢您的回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
  • 2018-02-06
  • 2010-09-17
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多