-1

I have a php page that is supposed to update a record in a database. But for some weird reason, its not updating and I can't seem to figure out what's wrong. Maybe another pair of eyes can see what I'm missing.

if (isset($_POST['search'])) {

    $refnum = $_POST['refnum'];
    $paymentstatus = "Paid";
}

if ((isset($_POST["form_update"])) && ($_POST["form_update"] == "paymentconfirm")) {

    $stmt = $connQlife->prepare("UPDATE cimbooking SET paymentstatus=? WHERE bookingrefnum=?");
    $stmt->bind_param('ss', $paymentstatus, $refnum);
    $results = $stmt->execute();
    $stmt->close();

    if($results){
        $updateGoTo = "confirm"; 
        $errorGoTo = "error";
        if (isset($_SERVER['QUERY_STRING'])) {
            $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
            $updateGoTo .= $_SERVER['QUERY_STRING'];
        }
        header("Location: ". $updateGoTo);
    }else{
            header("Location: ". $errorGoTo);
    }
}

I just get redirected to the confirmation page that says it was successful. But it's not updating the table.

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

Well i've been able to find a fix. The page when it loads first gets the value of refnum from the previous submitted page and stores it in the variable $refnum.

if (isset($_POST['search'])) {

    $refnum = $_POST['refnum'];
    $paymentstatus = "Paid";
}

Now this current page has another form for updating the record in the database and is supposed to use the value of the $refnum and it submits to itself.

if ((isset($_POST["form_update"])) && ($_POST["form_update"] == "paymentconfirm")) {

$stmt = $connQlife->prepare("UPDATE cimbooking SET paymentstatus=? WHERE bookingrefnum=?");
$stmt->bind_param('ss', $paymentstatus, $refnum);
$results = $stmt->execute();
$stmt->close();

if($results){
    $updateGoTo = "confirm"; 
    $errorGoTo = "error";
    if (isset($_SERVER['QUERY_STRING'])) {
        $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
        $updateGoTo .= $_SERVER['QUERY_STRING'];
    }
    header("Location: ". $updateGoTo);
}else{
        header("Location: ". $errorGoTo);
}
}

What was happening is that, because it was submitting to itself, the value of the $refnum was being lost.

So my fix (probably not the best way) was to create a session variable with the value of the $refnumso that when the page submitted to itself, i could then retrieve the value of the $refnum from the session and use it to update the record.

if (isset($_POST['search'])) {

    $refnum = $_POST['refnum'];    
    $_SESSION['REFNUM'] = $refnum;
}

if ((isset($_POST["form_update"])) && ($_POST["form_update"] == "paymentconfirm")) {

        $paymentstatus = "Paid";
        $newrefnum = $_SESSION['REFNUM'];

        $query = ("UPDATE cimbooking SET paymentstatus=? WHERE bookingrefnum=?");
        $stmt = $connQlife->prepare($query);
        $stmt->bind_param('ss', $paymentstatus, $newrefnum);
        $results = $stmt->execute();
        $stmt->close();

        unset($_SESSION['REFNUM']);

        if($results){
            $updateGoTo = "confirm"; 
            $errorGoTo = "error";
            if (isset($_SERVER['QUERY_STRING'])) {
                $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
                $updateGoTo .= $_SERVER['QUERY_STRING'];
            }
            header("Location: ". $updateGoTo);
        }else{
                header("Location: ". $errorGoTo);
        }
    }

That worked for me. If anyone has a better way, please do provide. Someone else might just have this same problem and could benefit from our solutions.