php redirect problem

1Tsme1941

Member
Guys, what am I doing wrong? The database "expiry" value is > 0 but I go to expiredpage.html.
========================================================
the code:

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

echo "<center>";echo date('m/d/y');echo "<br />";

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

if($expiry==7) {echo "1 week remaining on contract<br />";}
if($expiry==1) {echo "1 day remaining on contract<br />";}

if ($expiry==0) { header("location:expiredpage.html"); exit; } // not = 0, but goes there

// Perform a query, check for error
$sql = "UPDATE numberstbl SET $expiry = $expiry-1 where id=$id";
if(mysqli_query($link, $sql)){ echo "expiry was updated successfully."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }
?>
 

Cromewell

Administrator
Staff member
See the docs, headers only work if they are the first thing output by the code. You have an echo before the header.
 

1Tsme1941

Member
Hi, guy. It's been awhile. Tried a different approach by just printing instead of redirect but not
happnin'.
Changed table record "expiry" to 370, 7, 1, and to 0, always prints as if 0 and never updates.
========================================================


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

echo "<center>";echo date('m/d/y');echo "<br />";

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

if ($expiry==7) { print "1 week remaining on contract<br />"; }
if ($expiry==1) { print "1 day remaining on contract<br />"; }
if ($expiry==0) { print "We're sorry but your contract has expired.<br />"; }
else {
$sql = "UPDATE numberstbl SET $expiry = $expiry-1 where id=$id";
if(mysqli_query($link, $sql)){ echo "expiry was updated successfully."; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }
}
?>
 

Cromewell

Administrator
Staff member
$expiry='expiry';
$sql = "UPDATE numberstbl SET $expiry = $expiry-1 where id=$id";
Because of $expiry being set to a string, all of your if conditions are false and you should enter the else. While in this case it does not matter, note that your if statements are not connected, ie if, else if, else. You may have intended to write it this way, or maybe not.

Since the else branch is being entered, your SQL will look like this:
"UPDATE numberstbl SET expiry = expiry-1 where id=id"

It most likely will not do what you want.

Code:
create table if not exists testtbl (id integer, numberfield integer);

insert into testtbl (id, numberfield) values (1, 100);
insert into testtbl (id, numberfield) values (2, 200);
insert into testtbl (id, numberfield) values (3, 300);

select * from testtbl;

update testtbl set numberfield = numberfield - 1 where id = id;

select * from testtbl;

Outputs:
idnumberfield
1100
2200
3300

idnumberfield
199
2199
3299

Notice each rows numberfield was decremented. So your query should be subtracting 1 from the expiry field in every single row of your table.
 

1Tsme1941

Member
Wow! Ok, the numberstbl only has 1 record. The update works aside from the conditions.
My problem is in not understanding the if, else documentation. Thanks
 

1Tsme1941

Member
Hi, working further with this php issue, my logic is to exit when/if the "expiry" in the database
"numberstbl" is 0. Otherwise messages are displayed if expiry equals 7 or 1 AND the update occurs.
The update code is proven. There is only 1 row in "numberstbl". The conditions are as googled.
Please, why the error message?
========================================================================
the code:

<?php
$link = mysqli_connect("localhost", "root", "", "numbersdb");
// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }
echo "<center>";echo date('m/d/y');echo "<br />";

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

if(($expiry==0) {
exit("We're sorry but your contract has expired")
} elseif($expiry==7) {
echo "1 week remaining on contract";
} else($expiry==1) {
echo "1 day remaining on contract";
}

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

=============================
result:

Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in C:\x on line 12
 
Top