PHP MySQL数据库插入问题

时间:2015-08-06 00:11:23

标签: php mysql mysqli

我正在使用PHP构建一个MySQL数据库,以便从NYTimes API中引入JSON数据。我已经构建了所有内容并且包含了下面的脚本但是由于某种原因,当我将记录插入数据库时​​,它似乎没有完成插入功能,或者实际上将记录插入到数据库中我我真的很困惑。任何关于为什么会发生这种情况或如何调整它的见解将不胜感激。

MakeDatabase.php -

<?php

function PullData($url,$adx_keywords,$title,$abstract)
{
    $json = file_get_contents("http://api.nytimes.com/svc/mostpopular/v2/mostviewed/arts,sports/30.json?api-key=1a8bc0eb977b14db91dea9318942608b%3A14%3A72549166");
    $json_decoded=  json_decode($json,true);

    foreach ($json_decoded['results'] as $articles){
        array_push($url,$articles['url']);
        array_push($adx_keywords,$articles['adx_keywords']);
        array_push($title,$articles['title']);
        array_push($abstract,$articles['abstract']);
    }

}

function MakeDatabase($conn,$db_NAME)
{


// Create database
        $sql_createDB = "CREATE DATABASE IF NOT EXISTS " . $db_NAME;

        if ($conn->query($sql_createDB) === TRUE) {
             echo "Database linked successfully <br>";
      } else {
        echo "Error creating database: " . $conn->error;
}
}

function createTable($tablename, $db_NAME, $conn, $fields)
{
    mysqli_select_db($conn, $db_NAME);

        $sql_create = "CREATE TABLE IF NOT EXISTS $tablename (
                id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
                $fields[0] TEXT,
                $fields[1] TEXT,
                $fields[2] TEXT,
                $fields[3] TEXT
                )";    

        if ($conn->query($sql_create) === TRUE) {
            echo "Table Articles created successfully <br>";
        } else {
            echo "Error creating table: " . $conn->error . "<br>";
        }

}

function insertRecords($array,$fieldname, $conn, $db_NAME)
{
    mysqli_select_db($conn, $db_NAME);
    foreach ($array as $records)
    {
        $sql_insert="INSERT INTO tbl_articles('$fieldname')"
                . "VALUES('$records')";

        echo $sql_insert . "<br>";

        if(mysqli_query($conn, $sql_insert))
        {
            echo "Records Inserted.";
        }
        else 
        {
            die('Error : ' . mysqli_error($conn) . "<br>");
        }
    }
}




?>

的index.php -

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        require 'MakeDatabase.php';

        $db_NAME="nytimesnews";
        $tblName="tbl_articles";
        $fields=array('url','adx_keywords','title','abstract');

        $servername = "localhost";
        $username = "root";
        $password = "";

        $url=array();
        $adx_keywords=array();
        $title=array();
        $abstract=array();

        // Create connection
        $conn = new mysqli($servername, $username, $password);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
} 

        MakeDatabase($conn,$db_NAME);
        PullData($url,$adx_keywords,$title,$abstract);
        createTable($tblName, $db_NAME, $conn, $fields);
        insertRecords($url, 'url', $conn, $db_NAME);
        insertRecords($adx_keywords, 'keywords', $conn, $db_NAME);


        $conn->close();
        ?>
    </body>
</html>

1 个答案:

答案 0 :(得分:4)

我不确定这是否可以解决问题,但insert查询中存在错误。

您的查询,

$sql_insert="INSERT INTO tbl_articles('$fieldname')" . "VALUES('$records')";

应该是什么,

$sql_insert = "INSERT INTO `tbl_articles` (`" . $fieldname . "`) VALUES ('" . $records . "')";

我也重新格式化以使其更清晰。

此外,打开error reporting如果它不是alreasdy,让你和我们更容易调试,这样你就知道会抛出什么错误,可以修复它们或告诉我们所以我们知道在哪里代码出错了。

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);