I'm trying to enter data fetched from a json into a database. I have fetched the data as a multidimensional array and used a foreach loop to loop through the arrays.
I'm trying to insert this data into a mysql database but I keep getting an error Error: Field 'summary' doesn't have a default value .
My database table is called articles in a database called tracking and has the following fields: article_id, url, domain, favicon, title, summary, likes, tweets, plusones, image, category
my php file is called json_parser.php and it is as 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 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>";
}
}
}
?>
How do I loop through the array items and display them on appropriate fields
First of all, you have an error in your code :
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
"; }
Then, keep in mind that you are INSERTING row with only (url, domain, favicon, title)
fields, and then some rows with only (summary)
and then other one with only (image)
.
I think you wan't to insert into the same row no?
Then if you wan't to make it works, you have to set a default value :
ALTER TABLE `articles` CHANGE `summary` `summary` TEXT NULL DEFAULT NULL;
[EDIT to answer to the Mutuma comment]
Your database isn't set correctly. You have few sumaries for one group of (url, domain, favicon, title)
.
So you have to create another table, like summaries
with (id, ref_article, summary)
...
And it hte same for images
!
Please reconsider the base structure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.