Make a value into NULL instead of empty in php [duplicate] - php

This question already has answers here:
MySQL and PHP - insert NULL rather than empty string
(10 answers)
Closed 3 years ago.
I can't make the empty value to be checked as NULL in database instead of leaving an empty space when I am inserting a value.
I don't want to leave an empty/blank space for an entry when it has no value.
So if there is no file selected, make that entry into NULL
Is it unnecessary to be in NULL?
if (empty($_FILES['logo']['name'])) {
$code = NULL;
} else {
}

It's a normal problem with null-able strings. For a quick solution, you can change your code to:
$code = NULL;
if( !empty($_FILES['logo']['name']) ){
...
}
For more details, you can check this question:
MySQL and PHP - insert NULL rather than empty string

$logo = (!empty($_FILES['logo']['name']) ? "logo" : "NULL");
try this

Related

what's difference between double sign question mark(??) and single question mark [duplicate]

This question already has answers here:
PHP short-ternary ("Elvis") operator vs null coalescing operator
(15 answers)
Closed 2 months ago.
I still don't understand how to read this type checking with symbols ?? and only single ? and what's the difference. When i want to check the value of data, for an example i try this code:
//using double question mark
$course = isset($params->course_id) ?? NULL; //result that save in db is 0
//using single question mark
$course = isset($params->course_id) ? $params->course_id : NULL; //result that save in db is NULL
when i try to submit empty value the result is different, anybody can explain me?
The double question mark is the nullish-coaslescing operator and means - "If the value to my left is null or undefined... then return the content to my right"... (or perform the function to my right if thats what you have there)
The single question mark is part of a ternary operator and is paired with the colon symbol. The question mark evaluates to true and the colon to false (another way of thinking of this is the question mark is shorthand for an if block and the colon is shorthand for the else block
//using double question mark
$course = isset($params->course_id) ?? NULL;
//means that is the preceding content is null or undefined - return NULL
//using single question mark the ? is the **if block** and the : is the **else block** and then assigning that value to the course$ variable
$course = isset($params->course_id)
? $params->course_id
: NULL;

I'm comparing user input with MySQL database but I'm getting wrong result [duplicate]

This question already has answers here:
Compare variables PHP
(5 answers)
Closed 1 year ago.
I'm writing a code to compare user input with the database. I want the code display "is phishing" when the user input a word that already exist on the database.
<?php
include 'backend.php';
$userinput = $_POST['userinput'];
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
if($userinput==isset($row['keywords']))
{
echo "is phishing";
}else{
echo "is legitimate";
}
}
?>
dont use isset function, it returns true/false, you are compairing a variable to a boolean value, if that data coming from data that means it already set, just remove isset function
The isset() function returns a boolean value that indicates if the parameter you provided is set or not. i.e. The parameter is not null or declared and never initialized.
To search for a value in an array you need to use the in_array() function, as described here. Therefore your code should be:
if(in_array($userinput, $row['keywords']))
{
echo "is phishing";
}else{
echo "is legitimate";
}
Be aware that in_array() IS case-sensitive. In that case:
$values = ["StackOverflow"];
in_array("StackOverflow", $values); //true
in_array("stackoverflow", $values); //false

PHP CSV League - How can I skip null value? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if a variable is empty
Simple PHP question:
I have this stement:
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
...//IF PASSWORD IS CORRECT STUFF WILL HAPPEN HERE
}
Somewhere above this statement I use the following line in my JavaScript to set the username as a variable both in my JavaScript and in my PHP:
uservariable = <?php $user = $_POST['user']; print ("\"" . $user . "\"")?>;
What I want to do is add a condition to make sure $user is not null or an empty string (it doesn't have to be any particular value, I just don't want it to be empty. What is the proper way to do this?
I know this is a sill question but I have no experience with PHP. Please advise, Thank you!
Null OR an empty string?
if (!empty($user)) {}
Use empty().
After realizing that $user ~= $_POST['user'] (thanks matt):
var uservariable='<?php
echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
Use empty(). It checks for both empty strings and null.
if (!empty($_POST['user'])) {
// do stuff
}
From the manual:
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)

Checking if input is not numeric - !is_numeric [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I have a short shop form. You can buy there some pens, mugs etc.
After you put a number of items you want to buy I am trying to validate the input information and if it is not correct just change it into 0.
Looks like the !is_numeric function doesn't work, because it always makes the amount 0.
Any help please? Does ! work with this function at all?
$mugAmount = Input::get('mugAmount');
if(!isset($mugAmount) OR $mugAmount = NULL OR !is_numeric($mugAmount) OR $mugAmount < 0){
$mugAmount = 0;
};
It is because of this $mugAmount = NULL. This should be $mugAmount == NULL.
= and == means totally different things ;)
ps: besides you can remove this comparison to NULL because if it is NULL then !isset($mugAmount) will be true :)

What is the PHP syntax to check "is not null" or an empty string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if a variable is empty
Simple PHP question:
I have this stement:
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
...//IF PASSWORD IS CORRECT STUFF WILL HAPPEN HERE
}
Somewhere above this statement I use the following line in my JavaScript to set the username as a variable both in my JavaScript and in my PHP:
uservariable = <?php $user = $_POST['user']; print ("\"" . $user . "\"")?>;
What I want to do is add a condition to make sure $user is not null or an empty string (it doesn't have to be any particular value, I just don't want it to be empty. What is the proper way to do this?
I know this is a sill question but I have no experience with PHP. Please advise, Thank you!
Null OR an empty string?
if (!empty($user)) {}
Use empty().
After realizing that $user ~= $_POST['user'] (thanks matt):
var uservariable='<?php
echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
Use empty(). It checks for both empty strings and null.
if (!empty($_POST['user'])) {
// do stuff
}
From the manual:
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)

Categories