1

書籍のタイトルや略歴情報が欠落している可能性のあるデータベーステーブルをクリーンアップしようとしています。ユーザーはボタンをクリックできるはずで、残りはプログラムが行います。データベースでクエリを実行すると、探している情報が返されるので、問題はforeachループにあると思います。これが私のコードです:

<?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);
?>

私にはすべてが正しく見えますが、firebugをオンにして実行すると、メッセージは表示されません。成功関数のConsole.log(data)は空の文字列を示しています。

私は何が間違っているのですか?for eachループを再構築する必要がありますか?

編集:更新されたレコードの数を正確にカウントするために、コードの一部を変更しました。これは$qtyArray[$ x] = $row['quantity']行です。console.log(data)は、2995レコードが更新されたことを示していますが、#messageは画面に表示されず、console.log(data)のみが表示されます。これがもう少し洞察を与えることを願っています。

4

2 に答える 2

1

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");
于 2012-06-13T20:27:22.323 に答える
1

エラーは while ループにある可能性があります。

while($row = $result->fetch_assoc()) {
        $isbnArray[$x] = $row['isbn13'];
        $sum = array_sum($isbnArray);
} // end of while loop

$x は 0 に初期化され、変更されることはないため、配列内の同じエントリを毎回上書きするだけです。

変更する必要があります:

$isbnArray[$x] = $row['isbn13'];

に:

$isbnArray[] = $row['isbn13'];
于 2012-06-13T19:52:39.290 に答える