问题
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