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" />
4
  • $_POST['send']? $_POST['email'] you mean? Commented Jul 23, 2014 at 19:15
  • What error are you getting? Also, it's "must have," not "must of." Commented Jul 23, 2014 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. Commented Jul 23, 2014 at 19:19
  • There is no code here that's checking to see if a value is entered in the $_POST['email'] variable. Commented Jul 23, 2014 at 19:19

2 Answers 2

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
    }
}
?>
Sign up to request clarification or add additional context in comments.

6 Comments

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 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.
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.
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
Ok code is updated. Not we check to make sure the form was submitted.
|
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
?>

2 Comments

@JSK No row is being inserted into the database table referrals?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.