1

我正在尝试清理可能缺少书名或生物信息的数据库表。用户应该能够单击一个按钮,而程序会完成其余的工作。我已经在我的数据库中运行了查询,它返回了我正在寻找的信息,所以我认为我的问题在于 for each 循环。这是我的代码:

<?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) 显示空字符串。

我究竟做错了什么?我应该为每个循环重组我的吗?

编辑:我更改了部分代码以准确计算更新了多少记录。这是 $qtyArray[$x] = $row['quantity'] 行。我的console.log(data) 显示更新了2995 条记录,但#message 没有出现在屏幕上,只是console.log(data)。希望这能提供更多的见解。

4

2 回答 2

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 回答
1

您需要在查询中转义您的 "

$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 回答