Emil_4 0 Newbie Poster

Hello,
So I have buttons on my site connected to a PHP code that updates the column "status" in "users" table with a value based on what button you press.
This is the PHP script for that:

<?php

include('functions.php');

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "emildeveloping";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$id = $_SESSION['user']['id'];

if (isset($_POST['108'])) {
$sql = "UPDATE users SET status = '10-8' WHERE id='".$id."'";
}

if (isset($_POST['107'])) {
    $sql = "UPDATE users SET status = '10-7' WHERE id='".$id."'";
    }

    if (isset($_POST['105'])) {
        $sql = "UPDATE users SET status = '10-5' WHERE id='".$id."'";
        }

        if (isset($_POST['1015'])) {
            $sql = "UPDATE users SET status = '10-15' WHERE id='".$id."'";
            }

            if (isset($_POST['1023'])) {
                $sql = "UPDATE users SET status = '10-23' WHERE id='".$id."'";
                }

                if (isset($_POST['1097'])) {
                    $sql = "UPDATE users SET status = '10-97' WHERE id='".$id."'";
                    }

                    if (isset($_POST['1042'])) {
                        $sql = "UPDATE users SET status = '10-42' WHERE id='".$id."'";
                        }

if (mysqli_query($conn, $sql)) {
    echo "Status was updated.";
    header( "refresh:2;url=index.php" );
} else {
    echo "Error: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

And this is the HTML snippet with buttons:

 <form  method="post" action="status.php">
   <input type="submit" class="submit-108" name="108" value="10-8">
</form>

<form  method="post" action="status.php">
   <input type="submit" class="submit-107" name="107" value="10-7">
   </form>

   <form  method="post" action="status.php">
   <input type="submit" class="submit-105" name="105" value="10-5">
   </form>

   <form  method="post" action="status.php">
   <input type="submit" class="submit-1015" name="1015" value="10-15">
   </form>

   <form  method="post" action="status.php">
   <input type="submit" class="submit-1023" name="1023" value="10-23">
   </form>

   <form  method="post" action="status.php">
   <input type="submit" class="submit-1097" name="1097" value="10-97">
   </form>

 <form  method="post" action="status.php">
   <input type="submit" class="submit-1042" name="1042" value="10-42">
   </form>

This part works fine, the column gets updated and so on.
The thing is that I have a div on my site that have a php snippet in it that displays the data from the status column with session variable.
And this div dosen't update when I have updated the column status and refreshes the page?
How can I make this happen?
The HTML snippet for displaying the status column:

 <div id="status" class="status"><b>My Status: </b><font color="#6195ff"><?php echo $_SESSION['user']['status']; ?></div></font>

Best Regards Emil

Dani AI

Generated

sessions do not auto-sync with your database. You write the new value to users.status, but the page still prints $_SESSION['user']['status'], which keeps the old value until you explicitly change it (or rebuild the session data). A simple fix is to either: (a) update the session right after the DB update, or (b) stop storing status in the session and read it from the DB each page load.

If you want to keep the session approach, update it and use a proper POST-Redirect-GET so the UI refreshes cleanly:

// status.php
session_start(); // make sure this runs
require 'db.php'; // open $conn (mysqli)
$allowed = ['10-8','10-7','10-5','10-15','10-23','10-97','10-42'];
$status  = $_POST['status'] ?? null;
if (!in_array($status, $allowed, true)) { http_response_code(400); exit('Invalid status'); }

$stmt = $conn->prepare('UPDATE users SET status = ? WHERE id = ?');
$stmt->bind_param('si', $status, $_SESSION['user']['id']);
$stmt->execute();

$_SESSION['user']['status'] = $status;      // keep session in sync
header('Location: index.php');               // PRG pattern, no echo before this
exit;

And simplify the HTML to one form with named buttons:

<form method="post" action="status.php">
  <button type="submit" name="status" value="10-8">10-8</button>
  <button type="submit" name="status" value="10-7">10-7</button>
  <button type="submit" name="status" value="10-5">10-5</button>
  <button type="submit" name="status" value="10-15">10-15</button>
  <button type="submit" name="status" value="10-23">10-23</button>
  <button type="submit" name="status" value="10-97">10-97</button>
  <button type="submit" name="status" value="10-42">10-42</button>
</form>

Alternative: do not store status in the session at all. On index.php, query SELECT status FROM users WHERE id = ? and echo that value. It avoids stale data and keeps the session small. Either way, ensure session_start() runs before reading/writing $_SESSION.

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.