Home > Mobile >  how to use database data to determine what action to take
how to use database data to determine what action to take

Time:07-14

I am looking to have a page with a redirect based on what is in the database. I have a table called "nametable" and 2 columns "id" and "switch" The id never changes, only the switch entry does. Sometimes it will have "on" as the entry, and sometimes it will have "off" as the entry (depending on what I enter in there at the time)

So I want a page where the website visitor will go to, and if the database says "on" then they will be redirected to, lets say "pageon.php" and if it says off, the visitor will be redirected to "pageoff.php"

I managed to do a simple echo to show on or off in text on the page. But I don't have the foggiest on how to have them redirected based on that.

Any thoughts on what I should search for to make this happen? And advice is appreciated.

PS. I tend to get a -1 because the site thinks I'm not specific on what I am wanting to do. If I am unclear, please tell me so I can revise before closing or -1

Thank you

EDIT: Based on the advice I was given in the comments, I have made this so far. I'm only getting a blank page though. Any thoughts?


    <?php
    $servername = " ";
    $username = " ";
    $password = " ";
    $dbname = " ";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, switch FROM nametable";
    $result = $conn->query($sql);
    
    
    if ($row['switch'] == "on") header("Location: off.php"); else header("Location: on.php");
    
    
    
    $conn->close();
    ?>

 

CodePudding user response:

Try this:

<?php
    $servername = " ";
    $username = " ";
    $password = " ";
    $dbname = " ";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, switch FROM nametable";
    $result = $conn->query($sql);
    
    $row = $result->fetch_assoc();
    
    if ($row['switch'] == "on"){ 
        header("Location: off.php"); 
    } else { 
        header("Location: on.php"); 
    }
    
    $conn->close();
    
?>

CodePudding user response:

I got it. Thanks to the help in the comments, and the answer by @Edgaras except I made a tiny switch to have the == off, and that made it work. thank you all so much. Here is the solution.

<?php
    $servername = " ";
    $username = " ";
    $password = " ";
    $dbname = " ";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, switch FROM nametable";
    $result = $conn->query($sql);
    
    $row = $result->fetch_assoc();
    
    if ($row['switch'] == "off"){ 
        header("Location: off.php"); 
    } else { 
        header("Location: on.php"); 
    }
    
    $conn->close();
    
?>
  • Related