PHP and MySQL problem - multiple images path not saving in database

Hi, I am new here. I am really stuck and would appreciate some help.
I am having bit of trouble with my coding. Right now, I have images being downloaded on my local server from a website, along with that some of articles are being saved in the database.

Now, in my coding, only trouble I have having is that my code is only saving a path of single image in all of database records. What I want to achieve is that each picture which is saved, their path is saved in database with its marching article (each article has 1 picture and correct image path needs to be saved in correct row).

PHP code:

[php]<?php

$doc = new DOMDocument();
$doc->load(‘http://yourwebsite.com/index.xml’);

$xpath = new DOMXpath($doc);
$nodeLists = $xpath->query (’//ContentItem[@Href]’);
foreach( $nodeLists as $node )
{
$itemRSS = array (
‘title’ => $node->parentNode->getElementsByTagName(‘title’)->item(0)->nodeValue,
‘ContentItem’ => $node->getAttribute(‘Href’),
);

$arrFeeds[] = $itemRSS;
}

$localPath = ‘C:\xampp\htdocs\image’;
foreach( $arrFeeds as $feed )
{
$url = parse_url( $feed[‘ContentItem’] );
parse_str( $url[‘query’], $query );
$localName = “$localPath/{$query[‘k’]}.jpg”;
if( file_exists( $localName ) )
{
echo “Already downloaded: ‘$localName’”.PHP_EOL;
}
else
{
$data = file_get_contents( $feed[‘ContentItem’] );
if( ! $data )
{
echo “Error downloading ‘{$feed[‘ContentItem’]}’”.PHP_EOL;
}
else
{
if( ! file_put_contents( $localName, $data ) )
{
echo “Error saving ‘$localName’”.PHP_EOL;
}
else
{
echo “’$localName’ successful saved”.PHP_EOL;
}
}
}
}

$arrFeeds = array();
foreach ($doc->getElementsByTagName(‘item’) as $node) {
$itemRSS = array (
‘title’ => $node->getElementsByTagName(‘title’)->item(0)->nodeValue,
‘abstract’ => $node->getElementsByTagName(‘abstract’)->item(0)->nodeValue,
‘link’ => $node->getElementsByTagName(‘link’)->item(0)->nodeValue,
‘keywords’ => $node->getElementsByTagName(‘keywords’)->item(0)->nodeValue,
‘body’ => $node->getElementsByTagName(‘body’)->item(0)->nodeValue,
‘headline’ => $node->getElementsByTagName(‘headline’)->item(0)->nodeValue,
);
array_push($arrFeeds, $itemRSS);
}

$mysqli = new mysqli(‘localhost’, ‘root’, ‘’, ‘testdb’);

if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}

$check=mysqli_query($mysqli,“select * from testi where title=title”);
$checkrows=mysqli_num_rows($check);

    $stmt = $mysqli->prepare("select * from `testi` where title='?'");

    if($checkrows > 0){

echo “article already exists.”;
}else{
// do something
if (!$check)
{
die('Error: ’ . mysqli_error($mysqli));
}
}

if ($stmt = $mysqli->prepare(“INSERT INTO testi (title, abstract, link, keywords, body, headline, image)
VALUES (?, ?, ?, ?, ?, ?, ?)”)) {
$stmt->bind_param(‘sssssss’, $title, $abstract, $link, $keywords, $body, $headline, $localName);}

else {die("Errormessage: ". $mysqli->error);}

foreach( $arrFeeds as $RssItem){
$title = $RssItem[“title”];
$abstract = $RssItem[“abstract”];
$link = $RssItem[“link”];
$keywords = $RssItem[“keywords”];
$body = $RssItem[“body”];
$headline = $RssItem[“headline”];
$stmt->execute();
}

printf (" New Record has id %d.\n", $mysqli->insert_id);

$stmt->close();
$mysqli->close();

?>[/php]

I would really appreciate some help. Thank you.

You are binding the parameter before the loop starts, that’s one issue.

Thank you, astonecipher. I tried to change that, but still it doesn’t solve my problem. :frowning:

Post what you changed it to.

This is what I changed it to:

if ($stmt = $mysqli->prepare("INSERT INTO `testi` (`title`, `abstract`, `link`, `keywords`, `body`, `headline`, `image`) VALUES (?, ?, ?, ?, ?, ?, ?)")) 

 die("Errormessage: ". $mysqli->error);

foreach( $arrFeeds as $RssItem){
    $title = $RssItem["title"];
    $abstract = $RssItem["abstract"];
    $link = $RssItem["link"];
	$keywords = $RssItem["keywords"];
	$body = $RssItem["body"];
	$headline = $RssItem["headline"];
        $localName = $RssItem[""];
	$stmt->bind_param('sssssss', $title, $abstract, $link, $keywords, $body, $headline, $localName);
 $stmt->execute();
   }

printf (" New Record has id %d.\n", $mysqli->insert_id);

$stmt->close();
$mysqli->close(); 

Also, I am confused on this part:
$localName = $RssItem[""]; (What needs to go in there, because I tried a lot of things but it didn’t work, it says undefined index).

Sponsor our Newsletter | Privacy Policy | Terms of Service