The code below prints out a dropdown box with the default string being --Select Status-- with a value of NULL.
print("<label>Overall Status Overwrite:</label>
<select name='case_ov_status' class='case_ov_status'>
<option selected='selected' value=NULL>--Select Status--</option>");
When the form has been submitted, it will call a function which will determine whether to dropdown box as default value, if not I will update SQL database.
However, this is where I'm confused, the if statement below will still RUN despite submitting a value of NULL.
I found out 2 hours later that by using if($case_ov_status != 'NULL') instead of if($case_ov_status != NULL) solved the problem.
if($case_ov_status != NULL){ //Still ran despite != NULL.
mysql_query("START TRANSACTION", $connection);
$sql = "Update cases set status=".$case_ov_status." Where patientid='".$patientID."' and caseid='".mysql_real_escape_string($case)."'";
$resultNew = mysql_query($sql, $connection);
This may sound really basic, but can someone explain how this works? As I am still learning..
Thanks in advance!
Please read http://php.net/manual/en/language.types.null.php
NULL is a special value to represent unallocated objects in php. "NULL" is a string literal containing the word NULL and is not the same. In PHP however you may see that it will do some type juggling with equality. For example, "" == NULL is true.
You cannot send a PHP NULL value over HTTP since everything is a string in HTTP. So your form even though you specify NULL as a value, will actually send "NULL" to the server.
value=NULL is equivalent to value="NULL". You can't submit non-string values over HTTP. Instead of value=NULL use value="" (empty string). This is falsy for php just like NULL is.
All inputs are ALWAYS represented as string data.
You just have to fill your values with correct values, it cannot be of type null, neither int or smth else.
Possible workaround for you:
Cast every input to integer, if you expect an integer. Like $id = (int) $_GET['id'] ;
Use such functions as empty(). It will return true on "0", 0, null and some other.
you have gotten confused here.
NULL != 'NULL'
The null on the left is a reserved value for a variable that has no value,
The nul on the right is a string containing the word null.
Now to get to your example provided, if you changed your if statement to != 'NULL' (with quotes) you will get your expected result.
if($case_ov_status != "NULL")
My advice however would be to set that value in the option tag to value="" or value="0" and then change your if statement to either :
if($case_ov_status != "")
if($case_ov_status != 0)
or even better
if(!empty($case_ov_status))
when user select <option selected='selected' value=NULL>--Select Status--</option>");, it returns the value you specified in value attribute. it could be 1,2,abc,..or NULL. the string NULL returns as selected value.
when you check if($case_ov_status != NULL), it says $case_ov_status is set to NULL value (that is "") or not? but obviously you need to check if selected value is NULL string. so you have to say if($case_ov_status != 'NULL')
Related
My code looks like this:
$muted = 'true'; //Note this is only for testing
if ($muted == 'false' || $muted == '' || $do_not_text == '' || $do_not_text =='false'){
//do this first thing
}
else{
//do something else
}
I can't get my else to run. What syntax am I messing up?
Related to this, I'm storing a value in my database (that will be what's called to set $muted in my real code) that's either 'true', 'false', or ''. What data type should I be storing these as? Currently, I'm using VARCHAR, but I suspect this is all part of the problem.
$do_not_text == '' evaluates to true. Why? Because $do_not_text is not defined which is a falsy value. You are comparing it to an empty string which also equates to a falsy value. So that comparison is true causing the first if statement to be evaluated as true.
I'm not sure why you're using strings for what should be boolean values.
$muted = true; // no need for quotes
You might also consider using the === operator when comparing boolean values.
if ($muted === false || // etc...
What data type should I be storing these as?
Boolean values in MySQL are typically stored as 1 or 0.
field_name TINYINT(1) UNSIGNED NOT NULL DEFAULT 0
Store them as TINYINT with length of 1 because its only 1 and 0, and 0 as the default value.
Then you can make $muted = boolval($db_muted_val); if you want, or use $db_muted_val as is, because 1 is true and 0 is false.
if ($db_muted_val) {
// do this first thing
} else {
// do something else
}
I have a Boolean column in one of the database table called "status" that i would like to use to control the status of the content being submitted by users, i've set its type to TINYINT and the default value for it is '0' and am using this snippet to apply functionality to the content
$status = '';
if ($row_userDetails['status'] == FALSE) {
$status = 'Not Approved';
}
else {
$status = 'Approved';
}
var_dump($row_userDetails['status']) results are string(0) "0"
And then after i will use the status to display or not to display the content if approved or not respectively.
But my problem is, i can't seem to get a TRUE value i.e "Approved" when i echo $status, i only get the FALSE value i.e "Not Approved". Please help me out on this
You are experiencing a discrepancy due to type-juggling.
It is 99% likely that $row_userDetails['status'] is providing a (string) 'FALSE'
^^ If this is true then use: if($row_userDetails['status'] === 'FALSE') <-- notice the single quotes denoting a string value
The proper way to figure out what type you are working with is to use:
echo gettype($row_userDetails['status']);`
and it will display either boolean, integer, double, string, array, object, resource, or NULL as referenced here
I have a php project that uses mysqli to query a database. Some of the columns in this database can be null. I have code that looks something like this:
$query = "...";
$result = $DB->query($query);
$row = $result->fetch_assoc();
$column = $row['mycolumn'];
If mycolumn is null, the value of $column appears to be the string, "NULL" (NOT the null value, but actually the string containing the word "NULL"). So what happens if I have columns which actually have the string "NULL" in them? How can I differentiate?
Thanks!
Josh
EDIT:
Upon closer inspection, it appears that the string is actually a 5-characters string. The first 4 characters are "NULL", but the last character is 0x0d, the carriage return. This makes it a lot easier to detect, although I'm still curious if there's a less hack-y way than just doing string comparison.
Use an if condition to check with ===
if($row['mycolumn'] === null) {
echo 'Real Null';
} elseif($row['mycolumn'] == '') {
echo 'Blank';
}
You are looking wrong way. Instead of trying to detect wrong NULL value you have to find out why it is wrong and correct it.
Neither Mysql nor mysqli would return a literal string 'NULL' for a null value.
So, you need to find your own code which converts NULL value to "NULL\n" string either at writing or reading. Are you using raw mysqli as $DB or it's a sort of abstraction class? If so - I'd say problem is there.
After that you can easily read NULL value with strict comparison === as suggested in other answers (though I am not sure about libmysql installations).
seems the column have the data type as string.
If it is string,
we can check by following
if($row['mycolumn'] == '' || is_null($row['mycolumn']))
{
echo "Coulmn is NULL value";
}
else if($row['mycolumn'] == "NULL")
{
echo "Coulmn is NULL value as string";
}
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.
Here is my code:
if (isset($_POST['addmonths'])){
if (!empty($_POST['months'])){
if (is_numeric($_POST['months'])){
$monthstoadd = $_POST['months'];
if ($monthstoadd < 1){
mysql_query("UPDATE users SET months='lifetime' WHERE username='$lookupuser'");
echo "Successfully set " . $lookupuser . " to lifetime";
}elseif ($monthstoadd > 0){
$monthstoadd = $monthstoadd*2592000;
mysql_query("UPDATE users SET months=months+'$monthstoadd' WHERE username='$lookupuser'");
echo "Successfully added " . $_POST['months'] . " months to " . $lookupuser . "'s paid time.";
}else{
echo "Error.";
}
}else{
echo "Months need to be numeric. If you're trying to set lifetime, use 0.";
}
}else{
echo "You didn't enter anything.";
}
}
When I enter 0, it should set the user to lifetime, but instead it just echos You didn't enter anything. Not really sure how to fix this. Any ideas?
Entering 0 into a field registers as empty?
Yes, it does. From the docs for empty:
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)
Any ideas?
empty doesn't seem like a good match for the condition you're checking for (did they type something in). Since you're already apparently using a marker field to indicate that there should be a value there, you might consider trim and a check against a blank string:
if (trim($_POST['months']) != ''){
Since you'r using $_POST['months] in four different places, I'd probably also cache the trimmed version to a local variable.
In general, You can substitute empty for isset (which returns false only of the variable doesn't exist or is null) or array_key_exists (for array keys).
In this case, you can do
if (!isset($_POST['months']) || $_POST['months'] === "")
Alternatively
if (!array_key_exists('months', $_POST) || $_POST['months'] === "")
Since this is POST data, null will never be a value (the browser can either not send the input or it sends an empty string, which PHP translates to ""). Therefore, isset and array_key_exists are interchangeable. isset is, however, preferred, since it's not a function and therefore is faster to run (not mention it's faster to write it).
You need to check the type to make sure it is empty instead of registers as empty with ===.
if ($_POST['months'] !== ''){
This checks to see if it exactly equivalent to empty. If it isn't it passes.