1

I'm confused how to use isset and empty, I'm trying to write a simple api, there should be error saying which param is missing and return error too if the param is null.

What's wrong my statement below?

$email= isset($_POST['email']) ? mysqli_real_escape_string($mysqli, $_POST['email']) : '';
if(empty($email)) {
    echo 'email cannot be empty!'
}
4
  • where is the $email variable ? Commented Dec 30, 2017 at 4:05
  • stackoverflow.com/questions/4559925/… Commented Dec 30, 2017 at 4:07
  • @HastaDhana typo sorry Commented Dec 30, 2017 at 4:15
  • Do you really mean the param is null? Or do you mean the param is an empty string? Commented Jun 5, 2018 at 19:42

5 Answers 5

1

You don't actually need to use both isset and empty, because empty already does it.

No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

More details are here: http://php.net/manual/en/function.empty.php

So your code could look this way, for example:

if (empty(trim($_POST['email']))) {
    echo "Email cannot be empty!\n";
    // you should add return or raise exception here
    // or even exit
    exit;
}

$email = trim($_POST['email']);

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Your email {$email} is invalid\n";
    // you should add return or raise exception here
    // or even exit
    exit;
}

$email = mysqli_real_escape_string($email);
Sign up to request clarification or add additional context in comments.

4 Comments

so I have to echo twice, what if it is not email? it's fname or lname?
@felleciaWen you can isolate this logic in a method and just pass there which fields and how should be validated (a set of rules), bit if your are looking for something really simple, then yes: each field should be validated separately. On the other hand, you can validate if some variables are set or not in a loop.
can't I combine is set and is empty and return the same error msg? they are basically the same thing, is empty is to prompt to the user, is set is to prompt to the front end developer. Hope you get the point.
@felleciaWen I understand what you mean: it's possible frontend won't pass email at all OR will pass an empty string as email. My code will work for both cases and return the same error echo "Email cannot be empty!" You can surely change the text if you want. I made a few changes in my code and added trim to prevent an issue when users just enter spaces in a field (what should be treated as an empty email as well).
0

Change $email to :

$email= isset($_POST['email']) && !empty($_POST['email']) ? mysqli_real_escape_string($mysqli, $_POST['email']) : '';

So the $_POST['email'] is not containing empty value.

Comments

0

Try to use next code snippet

if( false === input_filter(INPUT_POST, FILTER_VALIDATE_EMAIL) ) {
    echo "error message";
}

$email = input_filter(INPUT_POST, FILTER_SANITIZE_EMAIL );
// to do somethink with email...

Comments

0

I think you are trying to check $email value which is null. You should check $fname in if statement instead.

1 Comment

fname is just a typo
0

try this to check for all missing fields:

 $required_fields = array('email', 'name', 'password');
$err_msgs=array();
foreach($required_fields as $field) { 
  if(empty( $_POST[$field]) ){
  err_msgs[]= $field . ' cannot be empty!';
    }
  }

  if (!empty($err_msgs)) {
  echo json_decode($err_msgs);
  }

Edit: removed isset() after reading Axalix's answer.

Comments

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.