dongpu2694 2014-07-23 19:13
浏览 25
已采纳

所需输入字段的PHP错误消息

I am trying to display an error message if a user has not entered a email and not add a column to the table.

The php code sends the email & randomly generated Code to the database and then displays a message, but if their has been not been a email entered I am trying to get a message to ask for an email and not create a column until done so.

I have achieved this before with other data inputing but I am still new to PHP, I have checked for answers but cannot just find the simple fix which I know it is.

Any help or comments would be greatly appreciated, thanks (Y)

PHP:

 <?php
 if(!empty($_POST['email'])) {
 ?>

   <p>Enter an email</p>

 <?php
  }

 if(isset($_POST['send'])){

 $email = mysqli_real_escape_string($conn,$_POST['email']);

$length = 10;
$inviteCode = "";
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
for ($p = 0; $p < $length; $p++) {
$inviteCode .= $characters[mt_rand(10, strlen($characters))];
 }

$query = mysqli_query($conn, "INSERT INTO `referrals` (`email`, `inviteCode`) 
 VALUES ('$email', '$inviteCode') ");

if($query){
?>
  <p> "Thank you"</p>
<?php 

  }
  else{
?>

   <p>Sorry there must have been a problem</p>

<?php
    die('Error querying database. ' . mysqli_error($conn));
   }
  } // end brace for if(isset($_POST['Login']))
  // end brace for if(isset($_POST['Login']))
?>

I have achieved this before by putting the following at the top;

 if (empty($_POST) === false) {
    $required_fields = array('email');
     foreach($_POST as $key=>$value) {
       if (empty($value) && in_array($key, $required_fields) === true) {
        $errors[] = 'Please Enter All Information';
        break 1;
       }
    }
  }

The html code for the submit and email input tags are below:

    <input type="text" placeholder="Email" name="email" />  
  <input type="submit" value="send" name="send" />
  • 写回答

2条回答 默认 最新

  • doupao2277 2014-07-23 19:21
    关注

    Add if(!empty($_POST['email'])) to the top of your logic, instead of isset($_POST['send']) unless you are doing something else with the post send value later in your logic. isset will be true if there is an empty string.

    To understand why please see:

    isset() and empty() - what to use

    Edit:

    <?php
    if(!empty($_POST)){
        if(!empty($_POST['email'])){
    
            $email = mysqli_real_escape_string($conn,$_POST['email']);
    
            $length = 10;
            $inviteCode = "";
            $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    
            for ($p = 0; $p < $length; $p++) {
                 $inviteCode .= $characters[mt_rand(10, strlen($characters))];
            }
    
             $query = mysqli_query($conn, "INSERT INTO `referrals` (`email`, `inviteCode`) VALUES ('$email', '$inviteCode') ");
             //you might want to consider checking more here such as $query == true as it can return other statuses that you may not want
             if($query){
             ?>
                 <p> "Thank you"</p>
             <?php 
            }
            else{
            ?>
               <p>Sorry there must have been a problem</p>
            <?php
                die('Error querying database. ' . mysqli_error($conn));
            }
        }
        else {
        ?>
            <p>Enter an email</p>
        <?php
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集