Hi, I'm having trouble coding the if/else statement. No matter what value the expiry has, line 17 is
echoed. If it is =>1 I want to sysnav.html.

<?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 propzmgmt@gmail.com"; }
else { header("location:sysnav.html"); } // line 18
?>

Recommended Answers

All 3 Replies

Try like this

if($expiry==0) { 
    echo "We're sorry but your contract has expired. Contact us immediately at propzmgmt@gmail.com"; 
} elseif($expiry >= 1) { 
    header("location:sysnav.html"); 
} 

And format the SQL correctly

$sql = "UPDATE `ctltbl` SET '$expiry' = `expiry` - 1 WHERE `id` = $id";
commented: Noice! +16

In your previous post, you mentioned that:

"ctltbl" is 1 row

That leads me to assume that the row has only two values id and expiry, so that $expiry in line 10 refers to the expiry value in the result.

If so, try this (tested):

<?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($link, "SELECT `id`, `expiry` FROM `ctltbl`");

$row = $results->fetch_row();

$id = $row[0];
$expiry = (int) $row[1];

if ($expiry > 0) {
  $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);
  }

  header("location:sysnav.html");
}

echo "We're sorry but your contract has expired. Contact us immediately at propzmgmt@gmail.com";
?>

You have some errors:

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

then

$sql = "UPDATE ctltbl SET $expiry = expiry - 1 where id=$id";

translates to:

$sql = "UPDATE ctltbl SET expiry = expiry - 1 where id=id"; // nonsensical as id=id is always true and update decrements all expiry by 1

Instead extract the data from the table info

$id = 3; // say, as this is missing and needs to be read in or looped through

$results = mysqli_query($link,"SELECT * FROM ctltbl where id=$id");
$expiry=MySQLi_fetch_array($results)

then test that

if ($expiry['expiry'] == 1) {
  echo "contract is expired";
} else {
  header("location:sysnav.html");
}

SQL is not meant to be used to update PHP variables outside the SQL script like you did in a nonsensical manner.

commented: Beautifully broken down and explained. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.