-2

I wanna update sql table with current active $_session but it does not work. I don't know where I went wrong. This is my code.

if (isset($_POST["submit"])) {

    $country = $_POST["country"];
    $fname = $_POST["fname"];
    $mname = $_POST["mname"];
    $lname = $_POST["lname"];
    $dob = $_POST["dob"];

    require_once 'connection.php';

    $userId = $_SESSION["userid"];

    $sql = "UPDATE users SET usersCountry = '$country', usersFname = '$fname', usersMname = '$mname', usersLname = '$lname', usersDob = '$dob' WHERE usersId = '$userId'";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("location: ../signupinfo.php?error=stmtfailed");
        exit();
    }

    mysqli_stmt_bind_param($stmt, "sssss", $country, $fname, $mname, $lname, $dob);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
    header("location: ../signupinfo.php?error=none");
    exit();
}

It doesn't seem to work when

$sql = "UPDATE users SET usersCountry = '$country', usersFname = '$fname', usersMname = '$mname', usersLname = '$lname', usersDob = '$dob' WHERE usersId = '$userId'";

But it works fine when

$sql = "UPDATE users SET usersCountry = '$country', usersFname = '$fname', usersMname = '$mname', usersLname = '$lname', usersDob = '$dob' WHERE usersId = '1'";

Where did I go wrong?

Dharman
  • 26,923
  • 21
  • 73
  • 125
  • Look at what the variable `$userId` contains - evidently it is not what you think it does. As indicated by the close reason there are bigger flaws with the code to address. – AD7six May 22 '22 at 08:52

0 Answers0