How to update MySQL Rows using PHP PDO Prepared Statements

隐身守侯 提交于 2020-01-06 05:26:37

问题


This is how I am receving data from a page:

 $response = file_get_contents("https://api.themoviedb.org/3/movie/550?api_key=xxxxxx");
    if ($response != FALSE) {
        $response = json_decode($response, true);
    }

And here is an example of what is inside the page:

{

  "genres": [
    {

      "name": "Action"
    },
    {

      "name": "Adventure"
    },
    {

      "name": "Science Fiction"
    }
  ]

}

Here is how, I Insert the data

$stmt = $conn->prepare("UPDATE genres SET genres_name = :genres_name WHERE tmdb_id = :tmdb_id");

    $stmt->bindParam(':tmdb_id', $tmdb_id,PDO::PARAM_INT);
    $stmt->bindParam(':genres_name', $genres_name,PDO::PARAM_INT);

if (isset($response["genres"]) && is_array($response["genres"])) 
{
    foreach ($response["genres"] as $genreObject) 
    {
        $genres_name = $genreObject["name"];
        $stmt->execute();
    } 
}

Original Result:

This is the data it update inside Genres table

tmdb_id         genres_name
5                Action
5                Action
5                Action

Expected Result:

tmdb_id         genres_name
5                Action
5                Adventure
5                Science Fiction

来源:https://stackoverflow.com/questions/45768731/how-to-update-mysql-rows-using-php-pdo-prepared-statements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!