Jump to content

PHP & MySQL - Not Inserting Rows when within Foreach loop


Recommended Posts

I'm using a foreach loop to output all of the information from an array. Within this while loop i have a MySQL query that is supposed to insert the outputted array into the database, each record on a new row as you would expect. Yet for some reason it refuses to write to a database at all. I know the MySQL query is because i have tried it outside of the Foreach loop and it inserts perfectly.

 

Not sure what could cause this or whether i'm over looking someone massivly obvious. Below is the code i'm using.

 

	foreach ($feed->get_items() as $item){
	$feed = $item->get_feed();
	$favico = $feed->get_favicon();
	$feedtitle = $feed->get_title();
	$link = $item->get_permalink();
	$title = $item->get_title();
	$content = $item->get_content();
	$date = $item->get_date('U');

	$favico = mysql_real_escape_string($favico);
	$feedtitle = mysql_real_escape_string($feedtitle);
	$link = mysql_real_escape_string($link);
	$title = mysql_real_escape_string($title);
	$content = mysql_real_escape_string($content);
	$date = mysql_real_escape_string($date);

	//Determine better source name
	if($feedtitle == "") {$source = "";}
	elseif($feedtitle == "Twitter / slaterjohn") {$source = "Twitter"; $prefix = "John Just Tweeted ";}
	elseif($feedtitle == "digg / johnslater / history") {$source = "Digg"; $prefix = "John Just Dugg ";}
	elseif($feedtitle == "Johnslater's Recently Played Tracks") {$source = "Last.fm"; $prefix = "John is Listening to ";}
	elseif($feedtitle == "Uploads from slater.john") {$source = "Flickr"; $prefix = "John just took a new Photo called ";}
	elseif($feedtitle == "John Slater's Facebook Posts") {$source = "Facebook"; $prefix = "John just posted ";}
	elseif($feedtitle == "slater.john - Latest News") {$source = "Blog"; $prefix = "John just posted ";}
	elseif($feedtitle == "slater.john - Latest Reviews") {$source = "Reviews"; $prefix = "John just reviewed ";}
	elseif($feedtitle == "slater.john - Articles") {$source = "Articles"; $prefix = "New Article called ";}
	elseif($feedtitle == "YouTube :: Videos by johnslater") {$source = "YouTube"; $prefix = "John has added the video ";}

	$query1 = "INSERT INTO `slaterjohn`.`minifeed` (`miniID`, `feedtitle`, `itemtitle`, `itemdate`, `itemlink`, `itembody`, `favico`) VALUES (NULL , '1', '2', '3', '4', '5', '6');";
	mysql_query($query1);

	echo $source.$title.$date.$link.$content.$favico.'<br />';

	} 

 

As you can see i have even put in 1,2,3,4,5,6 for the values and not even these will insert. Something is being strange.

 

 

Thanks in advanced.

		$query1 = "INSERT INTO `slaterjohn`.`minifeed` (`miniID`, `feedtitle`, `itemtitle`, `itemdate`, `itemlink`, `itembody`, `favico`) VALUES (NULL , '1', '2', '3', '4', '5', '6')";
	mysql_query($query1);

 

Try that, I took out the first ;  in the query.  you dont need it

That doesnt make any difference. From what i can gather i believe the query is fine - i just think im missing some secret code that allows me to insert rows into a database using loops which i possible surly.

 

Thanks for your help though :-)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.