PHP/MySQL - 将 JSON 数据插入 Bluehost 服务器上的数据库,空数据库条目

标签 php mysql json database bluehost

这是一个一次性脚本,用于将 JSON 数据插入到我在 Bluehost 上的 MySQL 数据库中。我在循环内外使用了各种 echo 语句来确保 JSON 信息被正确解析并且循环按预期工作。 Bluehost 帮助文件告诉我使用 SQL 语句而不是 SQLi 或 DBO。

<?php
    $con = mysql_connect ("localhost:port", "username", "password");
    if (!$con) {
        die('Could not reach database: Error Code ' . mysql_error() . "<br>");
    } else {
        echo 'Connected to database. ' . "<br>";
    }
    mysql_select_db ("db_name", $con);

    $jsondata = file_get_contents('BFZ.json');
    $data = json_decode($jsondata, true);

    $cards = $data['cards'];

    // loop through the cards array and load each set of variables into the DB
    for($i = 0; $i <= count($cards); $i++) {
        $card_Name = $cards[$i]['name'];
        $card_ManaCost = $cards[$i]['manaCost'];
        $card_CMC = $cards[$i]['cmc'];

        // Clear the variable from its last use
        $card_Colors = "";

        // If the card has no color info, assign the text "Colorless"
        if ($cards[$i]['colors'] == "") {
            $card_Colors = "Colorless";
        // Else if the card has color info, convert the colors array into one long text variable
        } else {
            for($colorIndex = 0; $colorIndex < count($cards[$i]['colors']); $colorIndex++) {
                $card_Colors = $card_Colors . $cards[$i]['colors'][$colorIndex] . " ";
            }
        }

        // various bits to load into the DB
        $card_Type = $cards[$i]['type'];
        $card_Rarity = $cards[$i]['rarity'];
        $card_Text = $cards[$i]['text'];
        $card_Number = $cards[$i]['number'];
        $card_Power = $cards[$i]['power'];
        $card_Toughness = $cards[$i]['toughness'];
        $card_MultID = $cards[$i]['multiverseid'];
        $card_ID = $cards[$i]['id'];

        // insert the data into the cards table
        $sql = "INSERT INTO cards (card_ID, card_name, manaCost, cmc, colors, type, rarity, card_text, card_number, power, toughness, multiverseid)
        VALUES ('$card_ID', '$card_Name', '$card_ManaCost', '$card_CMC', '$card_Colors', '$card_Type', '$card_Rarity', '$card_Text', '$card_Number', '$card_Power', '$card_Toughness', '$card_MultID')";
    }
    if (!mysql_query($sql, $con)) {
        die('Error: ' . mysql_error());
    } else {
        echo "Data inserted correctly. <br>";
    }
    $con->close();
?>

最后一个“正确插入数据”总是触发,但我的数据库只有一个条目,其中 cmc、power、toughness、multiverseid 和 colors 条目中存储了 0。 card_ID 是我的主键,它是空白的。

要么数据库设置不正确,要么我在代码中遗漏了一些东西。我会包括我的数据库结构的屏幕截图,但我不确定这里是否允许这样做。排序规则在文本条目上设置为 utf8_general_ci 和 Varchar,在数字上设置为 int 或 smallint。 JSON 结构在这里:http://mtgjson.com/ .

直到两天前我才知道 PHP 和 MySQL,所以如果我遗漏了一些简单的东西,请原谅我。我读过的所有其他问题似乎都没有解决这个问题。

最佳答案

您的 mysql_query 在 for 循环之外,这就是它只执行一次的原因。应该是

for($i = 0; $i <= count($cards); $i++) {
    //
    //...
    // insert the data into the cards table
    $sql = "INSERT INTO cards (card_ID, card_name, manaCost, cmc, colors, type, rarity, card_text, card_number, power, toughness, multiverseid)        VALUES ('$card_ID', '$card_Name', '$card_ManaCost', '$card_CMC', '$card_Colors', '$card_Type', '$card_Rarity', '$card_Text', '$card_Number', '$card_Power', '$card_Toughness', '$card_MultID')";

    //----> HERE instead
    if (!mysql_query($sql, $con)) {
       die('Error: ' . mysql_error());
    } else {
       echo "Data inserted correctly. <br>";
    }
}

$card_Type = mysql_real_escape_string($cards[$i]['type']);
$card_Rarity = mysql_real_escape_string($cards[$i]['rarity']);

等等

关于PHP/MySQL - 将 JSON 数据插入 Bluehost 服务器上的数据库,空数据库条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33466110/

相关文章:

php - 需要帮助将默认文本值插入 mysql

mysql - 隐藏 vb6 中的连接字符串并保护 mysql 密码

javascript - ExtJs getTotalCount() 不返回存储中的记录数

ruby-on-rails - rails : Restrict API requests to JSON format

javascript - 如何使用 JStree 不重复文件夹?

php - 带有 Composer 的 Google API PHP 客户端

PHP 登录表单行为异常

PHP 进度条 : % Of 2 Numbers

mysql - SQL:假设,如果将不合适的数字添加到唯一标识符列,会发生什么情况?

MySQL : Updating Cells