-1

I am trying to show people their profiles from my database using PHP. But every time a button is clicked I want the person_id ($which_person) to go up by 1 so the next person's profile is shown. I don't know how to do that because every time I click a button the page reloads so the variable "which_person" gets reset to 1.

    <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <link href="assets-putinhouse/css/style1.css" rel="stylesheet" type="text/css">
            <link rel="preconnect" href="https://fonts.googleapis.com">
            <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
            <link href="https://fonts.googleapis.com/css2?family=Festive&display=swap" rel="stylesheet">
            <title>Put people in a house</title>
        </head>
        <body>
            <?php
               include "db/connection.php"; 
               $conn = create_connection();
               $which_person = 1; //This resets to "1" every time the page reloads
    
               $getSql = "select * from Person where person_id = 1;";
               $data_labels = mysqli_query($conn, $getSql)->fetch_all(MYSQLI_ASSOC);
            ?>
    
            <div class="pagesize">
                <h1>Put people in a house</h1>
                <img src="assets-putinhouse/images/create_account.png" alt="profilepic" class="image_person">
                <?php
                    foreach($data_labels as $labels)
                {
                ?>
                    <li class="labels" data-id="<?php echo $labels['person_id'];?>">
                        <?php echo $labels["firstname"] ?>
                        <br>
                        <br>
                        <?php echo $labels["secondname"] ?>
                        <br>
                        <br>
                        <?php echo $labels["gender"] ?>
                        <br>
                        <br>
                        <?php echo $labels["descriptie"] ?>
                    </li>
                <?php
                }
                ?>
                <br>
                <form method="GET">
                <input type="submit" class="slytherin_button" value="Slytherin" name="slytherin_button_name">
                <br>
                <input type="button" class="gryffindor_button" value="Gryffindor"  name="gryffindor_button_name">
                <br>
                <input type="button" class="hufflepuff_button" value="Hufflepuff"  name="hufflepuff_button_name">
                <br>
                <input type="button" class="ravenclaw_button" value="Ravenclaw"  name="ravenclaw_button_name">
                <br>
                <input type="button" class="nextperson_button" value="Go to next person">
    
                <p class="text_firstname">Firstname: </p>
                <p class="text_name">Name: </p>        
                <p class="text_gender">Gender: </p>   
                <p class="text_info">Info: </p>   
    
    
    
                <?php
                if(isset($_GET['slytherin_button_name'])) {
                    onFunc($conn, $which_person);
                }
                if(isset($_GET['gryffindor_button_name'])) {
                    offFunc();
                }
                function onFunc($conn, $wich_person){
                    $sqlUpdate = "UPDATE Person SET slytherin = slytherin + 1  WHERE person_id like '$which_person';"; //this does the value "slytherin" of which_person go up by 1;
                    mysqli_query($conn, $sqlUpdate);
                    $wich_person = $wich_person + 1; //which person goes up by 1 so the next person will be shown but gets reset at the start
                }
                function offFunc(){
                    echo "Button is clicked";
                }
                ?>
            </div>
            <script src="assets-putinhouse/js/script.js"></script>
        </body>
    </html>

´´´
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
JonB
  • 17
  • 5
  • If you want this for ALL people, you need to increment and store on the server and add it to the page – mplungjan Dec 18 '21 at 15:14
  • yes, my plan is to do it for every person in the database. It will stop once the last person has been showed. By store it on the server, do you mean I need to make a variable in the database and increment that one? – JonB Dec 18 '21 at 15:17
  • If you want other ppl to see the change, yes – mplungjan Dec 18 '21 at 15:24
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Dec 18 '21 at 15:47
  • 1
    This is incorrect. Primary keys need not be sequential. Resetting issue should be addressed later. – nice_dev Dec 18 '21 at 15:50
  • Two ways: 1) User level - just store the variable in Cookie and change it when user clicks it, or even better store a session variable. 2) Server level - if not too many users just store and read in file, it many and you know how to do that use DB. – Undry Dec 18 '21 at 19:13

1 Answers1

0

(Untested) Instead of:

$wich_person = 1;

It seems you could do something like:

if (isset($_GET["id"]) {
    $wich_person = (int)$_GET["id"] + 1;
}
else {
    $wich_person = 1;
}

and then add the following to your form:

<form action="?<?php echo $wich_person ?>" method="GET">
...
</form>

And also, unless I'm mistaken it seems you've forgot to add </form> to your code.

In any event, this code should add the current ID to your URL every time you press submit, where then it gets increased by 1.

Edit: This is intended as a quick solution based on the code structure you provided, but upon reflection not sure if it's exactly what you're looking for. More robust solutions could be had, such as cookies or sessionStorage.

Siege
  • 331
  • 1
  • 7
  • 1
    okay I might give this a try. Also `````` is indeed missing, I messed around with it before and putted it in commentary I might have acidentally deleted it. – JonB Dec 18 '21 at 15:32
  • This is incorrect. Primary keys need not be sequential. – nice_dev Dec 18 '21 at 15:49
  • @nice_dev The poster specifically said "I want the person_id ($wich_person) to go up by 1", and his code "$wich_person = $wich_person + 1" implies the structure is sequential. – Siege Dec 18 '21 at 16:06
  • 1
    I agree with you but OP seems to be unaware about this. – nice_dev Dec 18 '21 at 17:13