PHP/MYSQL: check if variable is empty - php

I want to prevent an empty value from going into a MySQL database
I'm using following but for some reason it is letting the empty values get through...
if (trim($var)= '' || !isset($var)){
header("Location:page.php?er=novariablel");
}
else {
...insert into database
}
Note, there is a bunch of complicated stuff that sets the value of var which is why I want to have both the ='' and the !isset because either might be the case.
Am I missing something with the or statement, i.e. it evaluates to false if both are true. Or what am I doing wrong?

You're lacking an = for your Equal Comparison Operator inside your if statement. Try:
if (trim($var) == '' || !isset($var)){

Try:
if (!isset($var) || empty(trim($var))){
empty() is a better way to check to see if a variable has no value. Just keep in mind that the following will return true:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

Try:
$var = trim($var);
if (!empty($var)){
//not empty
}

Try
if (strlen(trim($var)))==0

You should also have put a constraint in your database table attribute that it will not accept NULL values. Then your entry would have not been made in the database.

#John this is great advice.
empty(trim($var))
I've been programming for 6 years and I never thought of trimming the variable before checking to see if it's empty.

Related

PHP: Check if FORM values are empty

I'm checking to make sure all incoming $_POST values are not empty. is using isEmpty the best method. This is a second check prior to db entry, I'm already using a jquery form validate
if (!isEmpty($_POST['add']) || ($_POST['add']['type'] == 2 && !isEmpty($_POST['cpl']))):
// insert into db
else:
header("Location: /?req=register");
exit();
endif;
try this
if (isset($_POST['add']) AND $_POST['add'] != '')
you can chcek in many way using also NULL
Be careful, as empty (no such thing as isEmpty) considers some values as empty.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
You can just use if($_POST['add'] <> '') ...
you can use isset to see whether the variable is set or not. This will help in radio and checkbox if you have not selected.
(isset($_POST['add']) && $_POST['add'] != '')
You can also use !empty($_POST['add'])

PHP conditional error testing for value

Is $var==1 the proper syntax for testing whether $var has a value?
I ask, because I'm getting a failure on a simple conditional.
Here's the code that seems to be failing:
if ($slcustom31==1) {$page="http://www.mydomain.com/members/page_Alpha.php";}
I assume that there's something wrong with the syntax, $var==1 .
++++++++
Here's the entire conditional, if that makes things clearer:
//condition 1
if ($slcustom31==1) {$page="http://www.mydomain.com/members/page_Alpha.php";}
//condition2
elseif ($slcustom31!=1 AND $slcustom29!="") {$page="http://www.mydomain.com/members/".$slcustom29;}
//condition3
else {$page="http://www.mydomain.com/members/page_Beta.php";}
sl_redirecttourl($page);
SPECIFIC SYMPTOM:
Script ignores condition1 and condition2, defaults to condition3.
++++
And if it helps, here's the entire (short) script:
$groupswithaccess="somegroup";
require_once("../slpw/sitelokpw.php");
require_once("../slpw/sitelokapi.php"); //gets value being tested by conditional
if ($slcustom31==1) {$page="http://www.mydomain.com/members/page_Alpha.php";}
elseif ($slcustom31!=1 AND $slcustom29!="") {$page="http://www.mydomain.com/members/".$slcustom29;}//not finished, but started
else {$page="http://www.mydomain.com/members/page_Beta.php";}
sl_redirecttourl($page);
Can someone tell me what I should do differently?
Thanks!
if($var ==1){
//returns true only if var is 1
}
if(isset($var)){
//returns true if you currently have any value stored as the variable $var
}
No, you want to look at isset or empty.
To check if a variable has a value use:
empty($var);
If it's 'empty' it will return TRUE, and if not will return FALSE.
This is what's considered empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
Fore more info see: http://php.net/manual/en/function.empty.php

How do I check if a $_GET parameter exists but has no value?

I want to check if the app parameter exists in the URL, but has no value.
Example:
my_url.php?app
I tried isset() and empty(), but don’t work. I’ve seen it done before and I forgot how.
Empty is correct. You want to use both is set and empty together
if(isset($_GET['app']) && !empty($_GET['app'])){
echo "App = ".$_GET['app'];
} else {
echo "App is empty";
}
empty should be working (if(empty($_GET[var]))...) as it checks the following:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
Here are your alternatives:
is_null - Finds whether a variable is NULL
if(is_null($_GET[var])) ...
defined - Checks whether a given named constant exists
if(defined($_GET[var])) ...
if( isset($_GET['app']) && $_GET['app'] == "")
{
}
You can simply check that by array_key_exists('param', $_GET);.
Imagine this is your URL: http://example.com/file.php?param. It has the param query parameter, but it has not value. So its value would be null actually.
array_key_exists('param', $_GET); returns true if param exists; returns false if it doesn't exist at all.

Checking if a $_COOKIE value is empty or not

I assign a cookie to a variable:
$user_cookie = $_COOKIE["user"];
How can I check if the $user_cookie received some value or not?
Should I use if (empty($user_cookie)) or something else?
Use isset() like so:
if (isset($_COOKIE["user"])){
$user_cookie = $_COOKIE["user"];
}
This tells you whether a key named user is present in $_COOKIE. The value itself could be "", 0, NULL etc. Depending on the context, some of these values (e.g. 0) could be valid.
PS: For the second part, I'd use === operator to check for false, NULL, 0, "", or may be (string) $user_cookie !== "".
These are the things empty will return true for:
"" (empty string)
0 (0 as an integer)
0.0 (0 as float)
"0" (0 as string)
NULL
FALSE
array() (an empty array)
var $var; (a declared variable not in a class)
Taken straight from the php manual
So to answer your question, yes, empty() will be a perfectly acceptable function, and in this instance I'd prefer it over isset()
If your cookie variable is an array:
if (!isset($_COOKIE['user']) || empty(unserialize($_COOKIE['user']))) {
// cookie variable is not set or empty
}
If your cookie variable is not an array:
if (!isset($_COOKIE['user']) || empty($_COOKIE['user'])) {
// cookie variable is not set or empty
}
I use this approach.
Try empty function in php
http://php.net/manual/en/function.empty.php
You can also use isset http://www.php.net/manual/en/function.isset.php
isset(), however keep in mind, like empty() it cannot be used on expressions, only variables.
isset($_COOKIE['user']); // ok
isset($user_cookie = $_COOKIE['user']); // not ok
$user_cookie = $_COOKIE['user'];
isset($user_cookie); // ok
(isset() is the way to go, when dealing with cookies)
You can use:
if (!empty($_COOKIE["user"])) {
// code if not empty
}
but sometimes you want to set if the value is set in the first place
if (!isset($_COOKIE["user"])) {
// code if the value is not set
}

Should I use null to see if a variable is empty in PHP?

Which is better to use when I use _GET['something here'] for a variable to check if it is empty or not
if (isset($_GET['url']) != '') {
//do stuff with it
}
OR
if (isset($_GET['url']) != NULL) {
//do stuff with it
}
'' or null or something else?
Please don't call this over optimizing or micro optimizing, I am simply looking for best practices, thank you
Use empty() - it actually first test of the variable exists before testing whether something is in it.
[Edit: isset() returns only TRUE or FALSE, so both of the statements above work equally well]
You should do the following to be sure that the value both exists and isn't empty
if(isset($_POST[myField]) && $_POST[myField] != "") {
Do my PHP code
}
PHP can be a little painful when debugging blank/missing/empty value checks. You can use empty() or isset(), but remember the cases where empty returns true. It's highly liberal with what it considers empty.
Returns FALSE if var has a non-empty and non-zero value.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
isset is more conservative in that it only checks for variable's existence and a NULL value. From the documentation:
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

Categories