我无法将json的一些值保存到我的数据库中

qlckcl4x  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(400)

我需要将json的一些值保存到我的数据库sql中。
数据库表“对抗”

这是我的php代码

require_once("database.php");
$apiURL = 'https://nico.planethoster.world/api-foot/confrontations/lire.php?annee=2020&semaine=29';
$response = file_get_contents($apiURL);
$jsonResponse = json_decode($response, true);
$items = $jsonResponse['match'];

    foreach ($items as $item ) {

        $query = "INSERT INTO `confrontations`(`id_match`, `id_equipe1`, `id_equipe2`, `cote1`, `coteN`, `cote2`, `date`, `heure`, `semaine`) 
        VALUES (:id_match, :id_equipe1, :id_equipe2, :cote1, :coteN, :cote2, :date, :heure, :semaine) ";
        $check = $pdo->prepare($query);
        $date = $item["date"];
        $heure = $item["heure"];
        $check->bindParam(':id_match', $item["id_match"], PDO::PARAM_INT);
        $check->bindParam(':id_equipe1', $item["id_equipe1"], PDO::PARAM_INT);
        $check->bindParam(':id_equipe2', $item["id_equipe2"], PDO::PARAM_INT);
        $check->bindParam(':cote1', $item["cote1"], PDO::PARAM_STR);
        $check->bindParam(':coteN', $item["coteN"], PDO::PARAM_STR);
        $check->bindParam(':cote2', $item["cote2"], PDO::PARAM_STR);
        $check->bindValue(':date', $date,  PDO::PARAM_STR);
        $check->bindValue(':heure', $heure, PDO::PARAM_STR);
        $check->bindParam(':semaine', $item["semaine"], PDO::PARAM_INT);

        $check->execute();

这是我的错误

mftmpeh8

mftmpeh81#

您正试图保存主键(id\u匹配)已存在的行。所以多次对峙使用相同的身份证匹配。
也许你已经运行过一次代码了。如果第二次运行它,就必须实现一些更新例程而不是插入。例如 UPDATE confrontations SET x = y WHERE id_match = z ;

相关问题