0

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" />
JSK
  • 177
  • 5
  • 15
  • $_POST['send']? $_POST['email'] you mean? – Stefan Jul 23 '14 at 19:15
  • What error are you getting? Also, it's "must have," not "must of." – elixenide Jul 23 '14 at 19:16
  • 2
    I believe you need to do additional checking of the post values. isset will always return true here it looks like. you need to make sure email is not an empty string. $email = ""; I would use if(!empty($_POST['email'])) as a better check. isset will be true for an empty string. – Shawn Jul 23 '14 at 19:19
  • There is no code here that's checking to see if a value is entered in the $_POST['email'] variable. – Nicholas Kouvatsos Jul 23 '14 at 19:19

2 Answers2

2

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
    }
}
?>
Community
  • 1
  • 1
Shawn
  • 3,583
  • 8
  • 46
  • 63
  • Thank you for your reply. I have updated the post, and still am not receiving a error message when using either script. If there's no email entered then don't submit and print a message that is all it should do :/ – JSK Jul 23 '14 at 19:38
  • JSK you are close. You don't want if(!empty($_POST['email'])) and then an error message. You want, if(empty(...)). That says if the variable is empty then show an error. My original line was how you had your initial code. – Shawn Jul 23 '14 at 19:44
  • Code is updated. This should work. If it does not then you will need to debug and var_dump($_POST['email']) and make sure theres not some value in there. – Shawn Jul 23 '14 at 19:52
  • Alright so this almost works, but the message enter an email is always displayed (even when first visiting the page) unless an email is entered and submitted – JSK Jul 23 '14 at 20:00
  • Ok code is updated. Not we check to make sure the form was submitted. – Shawn Jul 23 '14 at 22:12
  • Oh I see, thank you for your time and help !! It was appreciated – JSK Jul 23 '14 at 22:42
1

Try this:

<?php
 // check if email is empty, if it is display this message
 if(empty($_POST['email'])) {
 ?>

   <p>Enter an email</p>

 <?php
 }
 // else: the user entered something
 else {

   $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 else
 } // end else
?>
Nathan
  • 11,814
  • 11
  • 50
  • 93
  • @JSK No row is being inserted into the database table `referrals`? – Nathan Jul 23 '14 at 19:59
  • sorry my prev comment was wrong, it is the same as other answer. I am getting the enter an email message and does not add a column unless one is added - but I was trying to get the error message to only appear if they have tried to submit with an empty field. That way the user knows it did not submit – JSK Jul 23 '14 at 20:13