Insert Data into Mysql from PHP from a nested foreach loop -
i'm trying enter data fetched json database. have fetched data multidimensional array , used foreach loop loop through arrays.
i'm trying insert data mysql database keep getting error error: field 'summary' doesn't have default value.
my database table called articles in database called tracking , has following fields: article_id, url, domain, favicon, title, summary, likes, tweets, plusones, image, category
my php file called json_parser.php , follows
<?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 $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 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 $summary){ mysql_query("insert articles(summary) values('$summary')"); echo "$summary <br>"; } $images = $article_array['images']; foreach ($images $image){ $image_first= reset($image); echo $image_first; mysql_query("insert articles(image) values($image_first)"); echo "<img src=$image_first>"; } $social_shares = $article_array['social_shares']; foreach($social_shares $social_share=>$include){ echo $social_share . ": " . $include . "<br>"; } $entities = $article_array["entities"]; foreach($entities $entity_cat=>$entities_arr){ foreach($entities_arr $key=>$entity){ echo $entity_cat.' >> '.$entity. "<br>"; } } } ?>
how loop through array items , display them on appropriate fields
first of all, have error in code :
foreach ($large_summary $summary){ mysql_query("insert articles(summary) values($summary)"); echo "$summary
"; }
foreach ($large_summary $summary){ mysql_query("insert articles(summary) values('$summary')"); echo "$summary
"; }
then, keep in mind inserting row (url, domain, favicon, title)
fields, , rows (summary)
, other 1 (image)
.
think wan't insert same row no?
then if wan't make works, have set default value :
alter table `articles` change `summary` `summary` text null default null;
[edit answer mutuma comment]
your database isn't set correctly. have few sumaries 1 group of (url, domain, favicon, title)
.
so have create table, summaries
(id, ref_article, summary)
...
, hte same images
!
please reconsider base structure.
Comments
Post a Comment