PHP else condition not working

I’m stuck here, it won’t even go through else cond.
I have a form with action to go to the file with this code and a button that has type as submit.
I’ve tried copying the html form into the php file and changing the “status.update.inc.php” into “index.php” (it worked but i don’t like it transfering from page to page), also tried elseif doesn’t work
THE FORM :

<form action="backgroundcode/status.update.inc.php" method="GET">
      <button type="submit" name="submit" class="button"></button>
    </form>

THE IF STATEMENT :

<?php
  include_once 'dbh.inc.php';
  $setchecker = "SELECT status FROM status_table;";
  if (isset($setchecker) && $setchecker == 1){
    $sql1 = "UPDATE status_table SET status='1' WHERE id=1";
    $result1 = mysqli_query($conn, $sql1);
  } else{
    $sql1 = "UPDATE status_table SET status='0' WHERE id=1";
    $result1 = mysqli_query($conn, $sql1);
  }


?>

If anyone is interested dbh.inc.php is:

<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "status_checker";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

I also tried this:

<?php
  include_once 'dbh.inc.php';

  if ("SELECT status FROM status_table;" == 0){
    $sql1 = "UPDATE status_table SET status='1' WHERE id=1";
    $result1 = mysqli_query($conn, $sql1);
  } else{
    $sql1 = "UPDATE status_table SET status='0' WHERE id=1";
    $result1 = mysqli_query($conn, $sql1);
  }


?>

You are not running a query on this so $setchecker equals “SELECT status FROM status_table;”

The second example will also not work again as SELECT status FROM status_table; will never equal 0

I am not sure why the elses are not working ( possibly due to an error in the if? ) but you need to get your queries sorted.

Your form is not submiting anything either.

okay but my query is in the result1

$result1 = mysqli_query($conn, $sql1);

That is to update your table and not to get the value from “SELECT status FROM status_table”

Okay then, can I query and use the same variable ?

Not sure what you mean by that. You need to execute the first query, fetch the results from it, then run your if-else test on those results, not on the query string.

I am presuming that the idea of this is to “toggle” the value of status when someone presses a button on the form, as you don’t pass any values from the form. So because the first if is never true (because you’re comparing a string to a numeric value) the else clause works. I can only presume that you think it’s not working because the status column was zero to start with, and you just keep setting it to that value. Unless it’s something else, in which case please expand on “doesn’t work”.

If all you want to do is toggle the value, why not do something like:

update status_table set status = if(status=1, 0, 1) where id = 1

and let MySQL do the work? Or substitute true and false to use a Boolean column if those are the only two options.

1 Like

it worked, thanks a lot !!

(Purely hypothetical at this point, as the above answers the question fine)
I wonder if the database engine would prefer arithmetic operation to a functional logic one?

status = (status +1 )%2

now that u mention it, there is a minor bug. when i first load the site (or reload) the button does not do what it’s supposed to only after the 1st press it works (updates the database correctly)

What does your code look like currently?

What does it do on the first press that it should not, or what does it not do that it should?

<?php
  include_once 'dbh.inc.php';

$status_update = "UPDATE status_table SET status = if(status=1, 0, 1) where id = 1;";
$result = mysqli_query($conn, $status_update);
?>

So the button should always upon being pressed change the value in the database
if it’s 0 to 1 and if it’s 1 to 0 think of it as like wireless on/off switch for like a lamp. but on the first press it does not change the value it has in the database only after the initial press it does

I can’t see any reason why, any time you call that PHP code, it wouldn’t change the status. That would suggest to me that it’s something to do with the code that calls it, rather than the code you’ve shown.

As an aside, if it’s always going to be 1 or 0, it might be better to use a Boolean column type and use true and false.

Tbh i never learned boolean so that’s an unknown territory for me

It’s the same, it’s just a more appropriate data type. Instead of thinking in terms of zero or one and translating it in your head to mean “true” or “false”, you just use “true” and “false”. It’s a little irrelevant to the problem at hand.

That said, the column name of “status” doesn’t necessarily lend itself to boolean content - if you think of a column called “active”, then having a value of “true” or “false” in there immediately tells you whether the row / user / room / whatever is active or not.

The thing is this button is a prototype for a more complex set of buttons that will have maybe even 5+ possible values. I want to learn the easiest way possible to code any button doesn’t matter how many possible values it has, so using this now doesn’t really help me. it’s a bit complex to explain on a forum but you helped a ton, thanks. now i just have to troubleshoot the code.

Just figured out the problem :sweat_smile: it’s in the

<form action="status.update.inc.php" method="GET">

when i do redirect to status.update.inc.php on the site and reload it wont work on the first press so, correct me if i’m wrong, i have to check if someone got the page by reloading (if did redirect him back) or by pressing the button (keep him there)?

Oh, forget the Boolean option then - that would only work for a true or false situation.

I’m not really sure what you mean by that. Normally I would have expected that your PHP code to set the status value would be in a separate file, and you’d call that as part of your action parameter - the PHP code then runs the query (and whatever else it needs to do) and then it can redirect back to your calling page, or display a confirmation or whatever else you want. But you also say that you are redirecting to the status update page, which seems strange to me.

If you have the status update as a small standalone file, it makes it easier later on to call it using Ajax so there are no page reloads required.

What you would normally do in your status update page is to use the “POST” method rather than “GET”, and surround your code with:

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
   // do the update
  }

and in that way, if anyone navigates directly to your status update script, it won’t falsely change the status. Obviously once you’re passing values in, you’ll need to use the $_POST array instead of $_GET, but as you’re updating values, it’s more appropriate to use $_POST anyway.