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.

<?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"); }
?>

It looks like there are a few issues with your code. First, you are setting the value of the $id and $expiry variables to be the string 'id' and 'expiry' respectively. This means that when you use these variables in your UPDATE query, they will be used as the actual column names rather than the values you want to use.

Another issue is that you are using the mysqli_query function to execute your UPDATE query, but you are passing it the $link variable instead of the $mysqli variable that you created earlier.

Finally, your if statement at the end is using the assignment operator = instead of the comparison operator ==, so it will always evaluate to true and the code inside the if block will always be executed.

Here is how I would modify your code to fix these issues:

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

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

$id = 1; // Set the id value to the value you want to use
$expiry = 1; // Set the expiry value to the value you want to use

// Use the values of the $id and $expiry variables in the UPDATE query
$sql = "UPDATE ctltbl SET expiry = $expiry - 1 where id = $id";

// Execute the query using the $mysqli object
if($mysqli->query($sql)) {
    echo "Record was updated successfully.";
} else {
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}

// Use the comparison operator == to check if $expiry is equal to 0
if($expiry == 0) {
    header("location:expiredpage.html");
} else {
    header("location:sysnav.html");
}
?>

Hope this helps! Let me know if you have any other questions.

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.