Isset() Function Is Returning True Even When Item Is Not Set

isset() function is returning true even when item is not set

Null and/or empty strings are still set if the variable is declared. Try this:

if(isset($_POST['user_pass']) && $_POST['user_pass'] != "")

isset() is returning only true

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

isset()

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty()

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

is_null()

is_null — Finds whether a variable is NULL

In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.

In where shall I use isset() and !empty()

isset vs. !empty

FTA:

"isset() checks if a variable has a
value including (False, 0 or empty
string), but not NULL. Returns TRUE
if var exists; FALSE otherwise.

On the other hand the empty() function
checks if the variable has an empty
value empty string, 0, NULL or
False. Returns FALSE if var has a
non-empty and non-zero value."

php why is isset always true even if fields don't contain anything?

Null and/or empty strings are still set if the variable is declared. Try this:

if(isset($used_checkboxes_csv ) && $used_checkboxes_csv == 'somevalue')

PHP !isset returns true when not set

Normally, empty($var_name) would be more reliable, which will return true for anything null, blank, or undefined. This even includes empty arrays.

However, in this case, you are returning a DB object. Which will always return true for isset, and false for empty. So you are going to want to use the DB object API. In which case, you should use rowCount.

Button not being called with isset() function

As always... looks like StackOverflow users are toxic and unfriendly and don't even try to help new people who want to get into programming.

So I will try to help you a little bit.

If you are passing parameters via URL you have to use $_GET not $_POST method.

Also, I would change

<a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>

to

<a href='resultTablet.php?btnEditTablet={$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>

So you could do this:

if(isset($_GET["btnDeleteTablet"])){  

and you would get id via $_GET like this:

$idTablet=$_GET["btnDeleteTablet"];

After this change, you can close while loop you used before
this if(isset($_GET["btnDeleteTablet"])) line,
also you wont need $obj->id
anymore, because you will $_GET data decoupled from while loop or any other code you wrote before.

On another note, I see you forgot <tbody> </tbody> in your table.

EDIT:

Also, what are you doing with

 if(isset($_POST['btnSearchTablet'])){

This wont work after delete button click.

You shouldn't use it like that, because after the delete button click your page will go to URL with $_GET parameters and that if logic will prevent

 if(isset($_GET["btnDeleteTablet"])){  

logic working.
So move whole

if(isset($_GET["btnDeleteTablet"])){  
...
}

out of if(isset($_POST['btnSearchTablet'])){

Read up about $_POST and $_GET methods also, read about forms
you really need it.

Also, I recommend you to get program to profile requests so you could see how post and get data moves.
I recommend you to install Fiddler program, so you would be able to see how post, get data moves.

Ok, I will try to fix your code at last so it could work:

<?php

function searchTablet(){
global $connection;
if(isset($_GET['btnDeleteTablet'])){
deleteTablet();
}
//I dont why are you using it so I commented it out.
//if(isset($_POST['btnSearchTablet'])){
//}

displaySearchTablet();
}

function displaySearchTablet(){
global $connection;

$query = "SELECT * FROM tablet";
$run = mysqli_query($connection, $query);

while($obj = mysqli_fetch_object($run)){
//Combine all rows into one variable
$table_rows .= "
<tr>
<td>{$obj->id}</td>
<td>{$obj->idBrand}</td>
<td>{$obj->idModel}</td>
<td>{$obj->idColor}</td>
<td>{$obj->price}</td>
<td>{$obj->fabricationDate}</td>
<td>{$obj->idProvider}</td>
<td>{$obj->registrationDate}</td>
<td>
<a href='resultTablet.php?btnEditTablet={$obj->id}' class='btn btn-primary' name='btnEditTablet'>Alterar</a>
</td>
<td>
<a href='resultTablet.php?btnDeleteTablet={$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a>
</td>
</tr>";
}
$result_table =
"<table class='table table-striped'>
<thead>
<tr>
<th>ID</th>
<th>Brand</th>
<th>Model</th>
<th>Color</th>
<th>Price</th>
<th>Fabrication Date</th>
<th>Provider</th>
<th>Registration Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
$table_rows
</tbody>
</table>";
echo $result_table;
}
function deleteTablet(){
global $connection;

$id = $_GET['btnDeleteTablet'];
$query = "DELETE FROM tablet WHERE id = '$id'";
$run = mysqli_query($connection, $query);

if($run){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}

I kept it very basic, so you would be able to understand.
I didn't pass parameters or returned anything so it would be more understandable to you.

Good luck on your journey to programming.

PHP isset() returning false when should return true?

Remove the single quotes from the input names in your HTML so it reads:

name="formdata[active]"

Adding the single quotes would mean you would have to access the array in PHP as:

$_POST['formdata']['\'active\'']

or

$_POST['formdata']["'active'"]

which is highly inconvenient.

Why would a person want to use isset over just !$variableName

if we use directly $variableName and the $variableName is not defined before that check, then a Undefined variable warning will be generated by php. If we use isset, then if the variable is defined before, then it is set the isset will return true, if not defined then isset will return false, and hence no Undefined Variable or index warning will be generated.

It is a good practice to use isset with variables if there is a case that it is not defined before.

<?php
if(!$name){ //this will generate a warning
echo 'Name is not set';
}
if(isset($name) == false){ //this will not as isset returns false
echo 'Returned false';
}
?>


Related Topics



Leave a reply



Submit