Reputation: 1315
I am attempting to clean up a database table that might be missing book titles or bio information. The user is supposed to be able to click a button and the program does the rest. I have run the query in my database and it returns the information I am looking for, so i think my issue is with the for each loop. Here is my code:
<?php
require_once ('../db.php');
require_once ('../amazon/amazon.php');
$conn = db_connect();
session_start();
$x = 0;
// find all of the books with no Titles or Bios
$result = $conn->query("
select
i.date_created,
users.username,
i.sku,
i.isbn13,
i.quantity,
source.source,
i.date_process,
location.location
from inventory i
left join book on i.isbn13 = book.isbn13
left join source on i.source_id = source.source_id
left join location on i.location_id = location.location_id
left join users on i.created_by = users.user_id
where sku > '10000000'
and quantity >= 1
and (book.title = ''
or book.title is null
or book.author = ''
or book.author is null)
and i.isbn13 >1");
$num_rows = $result->num_rows;
if($num_rows > 0)
{
while($row = $result->fetch_assoc()) {
$isbnArray[$x] = $row['isbn13'];
$qtyArray[$x] = $row['quantity'];
$x++;
} // end of while loop
$sum = array_sum($qtyArray);
for each ($isbnArray as $isbn)
{
//retrieve amazon data
$parsed_xml = amazon_xml($isbn);
$amazonResult = array();
$current = $parsed_xml->Items->Item;
if($parsed_xml->Items->Request->IsValid == 'True') {
$amazonResult = array(
'Title' => $current->ItemAttributes->Title,
'Author' => $current->ItemAttributes->Author,
'Edition' => $current->ItemAttributes->Edition,
'Weight' => ($current->ItemAttributes->PackageDimensions->Weight / 100),
'Publisher' => $current->ItemAttributes->Publisher,
'PublishDate' => $current->ItemAttributes->PublicationDate,
'Binding' => $current->ItemAttributes->Binding,
'SalesRank' => $current->SalesRank,
'ListPrice' => str_replace('$','',$current->ItemAttributes->ListPrice->FormattedPrice),
'ImageURL' => $current->LargeImage->URL,
'DetailURL' => $current->DetailPageURL
);
} // end of if statement
//update Title and Bio info in book table
$conn->query("
update book
set isbn13 = '$isbn',
author = '" . $amazonResult['Author'] . "',
title ='" . $amazonResult['Title'] . "',
edition = '" . $amazonResult['Edition'] . "',
weight = '" . $amazonResult['Weight'] . "',
publisher = '" . $amazonResult['Publisher'] . "',
binding = '" . $amazonResult['Binding'] . "',
listed_price = '" . $amazonResult['ListPrice'] . "',
pub_date = '" . $amazonResult['PublishDate'] . "'
WHERE isbn13 = '$isbn'");
} // end of for each loop
}
$message = array( 'message' => $sum.' Records were updated' );
$conn->close();
echo json_encode($message);
?>
To me everything looks right, but when I run it with firebug on, there is no message. Console.log(data) in my success function says empty string.
What am I doing wrong? Should I restructure my for each loop?
EDIT: I changed parts of the code to get an accurate count of how many records were updated. This is the $qtyArray[$x] = $row['quantity'] line. My console.log(data) shows that 2995 records were updated, but the #message does not appear on the screen, just the console.log(data). Hope this gives a little more insight.
Upvotes: 1
Views: 255
Reputation: 245
You need to escape your " in your query
$result = $conn->query("
select
i.date_created,
users.username,
i.sku,
i.isbn13,
i.quantity,
source.source,
i.date_process,
location.location
from inventory i
left join book on i.isbn13 = book.isbn13
left join source on i.source_id = source.source_id
left join location on i.location_id = location.location_id
left join users on i.created_by = users.user_id
where sku > '10000000'
and quantity >= 1
and (book.title = \"\"
or book.title is null
or book.author = \"\"
or book.author is null)
and i.isbn13 >1");
Upvotes: 1
Reputation: 8049
Your error may lie in your while loop:
while($row = $result->fetch_assoc()) {
$isbnArray[$x] = $row['isbn13'];
$sum = array_sum($isbnArray);
} // end of while loop
$x is initialized to 0, and never changed, so you just overwrite the same entry in the array each time.
You have to change:
$isbnArray[$x] = $row['isbn13'];
to:
$isbnArray[] = $row['isbn13'];
Upvotes: 1