简体   繁体   中英

PHP PDO can't UPDATE

The same solution was working before but the parameters were not passed in JSON format and that's the only difference.

I even tried to write the SQL query manually with values not from variables and it's still giving back 0 rows affected.

What could cause this problem?

<?php
    $db = new PDO("mysql:host=localhost:3306;dbname=skistatus", "root", "");
    $data = json_decode(file_get_contents('php://input'), true);

    if ($_SERVER['REQUEST_METHOD'] == "GET") {
        $statement = $db->query('SELECT * FROM skilifts');
        $statement->setFetchMode(PDO::FETCH_ASSOC);
        echo json_encode($statement->fetchAll());
    }

    if ($_SERVER['REQUEST_METHOD'] == "PUT") {
        if($data['secret'] == "fee2c775c18a12b7b52b58129b00e1bd") {
            $sql = 'UPDATE skilifts SET `status` = :status WHERE `id` = :id';
            $query = $db->prepare($sql);
            $query->execute(array(":status"=>$data['status'], ":id"=>$data['id'] ));

            $count = $query->rowCount();
            if($count == '0'){
                echo "Failed";
                http_response_code(400);
            }
            else{
                echo "Success";
                http_response_code(200);
            }
        } else {
            echo $data['secret']." is not the magic word!";
            http_response_code(403);
        }
    }
?>

The parameter id wasn't being updated on the front-end before passing it to the back-end. Basically when the SQL query was executing, it was the same id with the same status value to be updated as the ones in the database, and therefore no rows were affected by the UPDATE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM