trouble w/ if

propz

New Member
Hi, this should be simple but I'm having trouble and need help, please. I just want to update the
"expiry" value in the "ctltbl" and relocate depending on the value.
The update doesn't happen and it goes to sysnav even when expiry is 0. "ctltbl" is 1 row.

The code:
<?php
//Open a new connection to the MySQL server
require_once "gethomedb.php";

//MySqli Select Query
$results = $mysqli->query ("SELECT * FROM ctltbl");

$id='id';
$expiry='expiry';

$sql = "UPDATE ctltbl
SET $expiry = expiry - 1 where id=$id";
if(mysqli_query($link, $sql)){ echo "record was updated successfully."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }

if($expiry=0) { header("location:expiredpage.html"); }
else { header("location:sysnav.html"); }
?>
 
Last edited:

Cromewell

Administrator
Staff member
You need ==. With single = it assigns 0 to expiry, then evaluates it. Since 0 is a false value in php, you go to the else branch.
PHP:
<?php
$a = 0;

if ($a = 1)
  echo "a is 1\n";
else
  echo "a is 0"; //prints

$a = 0;

if ($a == 1)
  echo "a is 1\n";
else
  echo "a is 0\n"; //prints
Output
Code:
a is 1
a is 0
 

Cromewell

Administrator
Staff member
So your code is basically
PHP:
....
$expiry=1;
...
if($expiry==0) { header("location:expiredpage.html"); }
And it went to expiredpage? Can you paste your revised code into a post?
 

propz

New Member
<?php
//Open a new connection to the MySQL server
require_once "gethomedb.php";

//MySqli Select Query
$results = $mysqli->query ("SELECT * FROM ctltbl");

$id='id';
$expiry='expiry';

$sql = "UPDATE ctltbl
SET $expiry = expiry - 1 where id=$id";
if(mysqli_query($link, $sql)){ echo "record was updated successfully."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }

if($expiry==0) { header("location:expiredpage.html"); }
else { header("location:sysnav.html"); }
?>
 

propz

New Member
this is the current code:
<?php
//Open a new connection to the MySQL server
require_once "gethomedb.php";

//MySqli Select Query
$results = $mysqli->query ("SELECT * FROM ctltbl");

$id='id';
$expiry='expiry';

$sql = "UPDATE ctltbl
SET $expiry = expiry - 1 where id=$id";
if(mysqli_query($link, $sql)){ echo "record was updated successfully."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }

if($expiry==0) { header("location:expiredpage.html"); }
else { header("location:sysnav.html"); }
?>
 

propz

New Member
Either you're toying with me or you don't know your ass from a hole in th ground. Will a valid moderator
or member help. I'm really trying to learn this. No matter what value the expiry has, line 17 is echoed.
If it is =>1 I want to sysnav.html.
my code:
<?php
$link = mysqli_connect("localhost", "root", "", "homedb");
// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }

//MySqli Select Query
//$results = $mysqli->query ("SELECT * FROM ctltbl");

$id='id';
$expiry='expiry';

$sql = "UPDATE ctltbl
SET $expiry = expiry - 1 where id=$id";
if(mysqli_query($link, $sql)){ echo "."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }

if($expiry==0) { echo "We're sorry but your contract has expired. Contact us immediately at [email protected]"; }
else { header("location:sysnav.html"); } // line 18
 

Cromewell

Administrator
Staff member
I just showed you the code I ran and execution results. If you are getting different results, then expiry is not what you think it is.

You can choose to believe I am clueless if you would like, but no one else is helping.
I can assure you I can in fact tell the difference between my ass and a hole in the ground.

And yes, I recognize the base of this code from your other account. I know who you are.
 

propz

New Member
Am I not welcome if I use my dad's machine? I am just trying to make sense of this code.

this is expiry:
id expiry receiptno
1 268 1212

my code:
<?php
$link = mysqli_connect("localhost", "root", "", "homedb");
// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }

//MySqli Select Query
//$results = $mysqli->query ("SELECT * FROM ctltbl");

$id='id';
$expiry='expiry';

if($expiry==0) { echo "We're sorry but your contract has expired. Contact us immediately at [email protected]"; }
else { header("location:sysnav.html"); }

$sql = "UPDATE ctltbl
SET $expiry = expiry - 1 where id=$id";
if(mysqli_query($link, $sql)){ echo "."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }
?>

the result:
We're sorry but your contract has expired. Contact us immediately at [email protected].
 

Cromewell

Administrator
Staff member
This is the same problem from all your code. Your variable actually contains 'expiry' not 1 or 0 or any other number. I have posted about this before.

In this case, for PHP prior to version 8 'any string' == 0 is true. But the real core issue is expiry is not what you think/say. It is not 1 or 0. So it is not working

When you are testing code do not initialize stuff with the types that it would not normally contain.

To see what compares to what and the results see https://www.php.net/manual/en/types.comparisons.php
 

Cromewell

Administrator
Staff member
Have you considered $expiry = -1; or $expiry = 0; or $expiry = 1000000000; or otherwise a number.

The other option is your test should change to something more appropriate for strings. It is hard to know what the intended data type is.

Also, this query will evaluate to the following line
PHP:
$sql = "UPDATE ctltbl
SET $expiry= expiry - 1 where id=$id";

//evaluated
$sql = "UPDATE ctltbl
SET expiry = expiry - 1 where id=I'd";
This will accidentally work with the test values you have defined. It is pretty much guaranteed to not work with real values.
PHP:
$id = 1234;
$expiry = '2022-11-20'; //I assume this is intended to be a date, maybe that is wrong
$sql = "UPDATE ctltbl
SET $expiry = expiry - 1 where id=$id";

//evaluated
$sql = "UPDATE ctltbl
SET 2022-11-20 = expiry - 1 where id=1";
 
Top