繁体   English   中英

从嵌套的foreach循环从PHP将数据插入Mysql

[英]Insert Data into Mysql from PHP from a nested foreach loop

我正在尝试将从json提取的数据输入数据库。 我已将数据作为多维数组获取,并使用了foreach循环遍历数组。

我正在尝试将这些数据插入mysql数据库,但是我一直收到错误消息:字段'summary'没有默认值

我的数据库表在称为跟踪的数据库中称为文章,并具有以下字段: article_id,URL,域,网站图标,标题,摘要,喜欢,推文,加号,图像,类别

我的php文件称为json_parser.php ,它如下

<?php
define('DB_NAME', 'tracking');
define('DB_USER', 'xxxxxxxxxxx');
define('DB_PASSWORD', 'xxxxxxxxx');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link){
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
    die('Cannot use '. DB_NAME . ': ' .mysql_error());
}

$url = "http://digitalrand.net/api/url_data/?key=abacus&pass=aba123cuxza%";
$json = file_get_contents($url);
$obj = json_decode($json, true);

foreach($obj as $article_array){

    $url = $article_array['url'];
    $domain = $article_array['domain'];
    $favicon = $article_array['favicon'];
    $title = $article_array['title'];
    $category = $article_array['category'];
    echo $category . "<br>";
    echo $domain . "<br>";
    echo $favicon . "<br>";
    echo $title . "<br>";
    echo $url . "<br>";

    $sql = 'INSERT INTO articles '.
               '(url, domain, favicon, title) '.
               'VALUES ( "$url", "$domain","$favicon","$title" )';
    if (mysql_query($sql)){
        echo "success.......";
    }


    if(!mysql_query($sql)){
       die('Error: ' . mysql_error());
    }

    $large_summary = $article_array['summary'];
    foreach ($large_summary as $summary){
        mysql_query("INSERT INTO articles(summary) VALUES('$summary')");
        echo "$summary <br>";
    }
    $images = $article_array['images'];
    foreach ($images as $image){        
        $image_first= reset($image);
        echo $image_first;
        mysql_query("INSERT INTO articles(image) VALUES($image_first)");
        echo "<img src=$image_first>";
    }
    $social_shares = $article_array['social_shares'];

    foreach($social_shares as $social_share=>$include){
        echo $social_share . ": " . $include . "<br>";
    }

    $entities = $article_array["entities"];

    foreach($entities as $entity_cat=>$entities_arr){
        foreach($entities_arr as $key=>$entity){
            echo $entity_cat.' >> '.$entity. "<br>";
        } 
    }
}   

?>

如何遍历数组项并将其显示在适当的字段上

首先,您的代码有错误:

foreach ($large_summary as $summary){ mysql_query("INSERT INTO articles(summary) VALUES($summary)"); echo "$summary
"; }

foreach ($large_summary as $summary){ mysql_query("INSERT INTO articles(summary) VALUES('$summary')"); echo "$summary
"; }

然后,请记住,您要插入仅包含(url, domain, favicon, title)字段的行,然后插入仅包含(summary)某些行,然后插入仅包含(image)其他行。
我想您不想插入同一行吗?

然后,如果您不希望它起作用,则必须设置默认值:

ALTER TABLE `articles` CHANGE `summary` `summary` TEXT NULL DEFAULT NULL;

[编辑以回答Mutuma评论]

您的数据库设置不正确。 一组(url, domain, favicon, title)摘要很少。

所以,你必须创建另一个表,就像summaries(id, ref_article, summary) ...
而且images也一样!

请重新考虑基本结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM