Kenny
Kenny

Reputation: 2144

PHP if keep executing, despite the logic

I've been banging my head on this for hours now trying to figure it out. I have a GET variable that basically goes into my database and toggles the ready field from 1 to 0 or 0 to 1, depending on what it currently is.

I use this url: change-status.php?id=1

Here's the print out of what I get after infinite page refreshes: 1 (status) [Yes Accessed] status=1 yes=1 1=rating 1=local id 0 (response) 1 (id)

I can't figure out why when I refresh it's not setting the status to 0. I went in mySQL and manually changed it to 0 in order to test the elseif, and it works. It's just the if part doesn't work. It actually enters the if statement, but it doesn't change the db at all.

<?php


require_once 'class/common.php';

$response;

$id = $_GET["id"];

$movies = new Movies();
$movies->get_status($id);

unset($status);
$status = $movies->ready;
$yes = 1;
$no = 0;

echo $status . " (status)<br>";

// Ready Yes, changing to Not Ready and return to AJAX 0
if ($status == $yes) {
    $movies->set_status($id, 0);
    $response = 0;
    echo "[Yes Accessed] status=".$status." yes=".$yes."<br>";
    echo $movies->ready."=rating<br>";
    echo $id."=local id<br>";
}
// Ready No, changing to Ready and return to AJAX 1
elseif ($status == $no) {
    $movies->set_status($id, 1);
    $response = 1;
    echo "[No Accessed] status=".$status." yes=".$yes."<br>";
}
else {
    $response = 404;
}

echo $response . " (response) " . $id . " (id)";


?>

Here is my db statement:

// Set Movie Status
public function set_status($id, $ready) {

    $statement = $this->database->prepare("UPDATE Movies SET Ready = ? WHERE ID = ?");
    $statement->bindParam(1, $id);
    $statement->bindParam(2, $ready);
    $statement->execute();

}

Thanks for any help anyone can provide, I just can't figure out why it's not working. It should be!

Upvotes: 1

Views: 33

Answers (1)

Barmar
Barmar

Reputation: 780889

Your parameters are in the wrong order in your bindParam() calls. It should be:

$statement->bindParam(1, $ready);
$statement->bindParam(2, $id);

I encourage you to switch to named parameters, then you wouldn't have this problem:

$statement = $this->database->prepare("UPDATE Movies SET Ready = :ready WHERE ID = :id");
$statement->bindParam(':id', $id);
$statement->bindParam(':ready', $ready);

Upvotes: 1

Related Questions