每个循环的php不起作用

每个循环的php不起作用,php,mysql,Php,Mysql,我正试图清理一个数据库表,该表可能缺少书名或生物信息。用户应该能够点击一个按钮,其余的由程序完成。 我已经在我的数据库中运行了这个查询,它返回了我正在寻找的信息,所以我认为我的问题在于for-each循环。 这是我的密码: <?php require_once ('../db.php'); require_once ('../amazon/amazon.php'); $conn = db_connect(); session_start(); $x

我正试图清理一个数据库表,该表可能缺少书名或生物信息。用户应该能够点击一个按钮,其余的由程序完成。 我已经在我的数据库中运行了这个查询,它返回了我正在寻找的信息,所以我认为我的问题在于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);
?>
Items->Item;
如果($parsed_xml->Items->Request->IsValid=='True'){
$amazonResult=数组(
'Title'=>$current->itemtattributes->Title,
'Author'=>$current->itemtattributes->Author,
'Edition'=>$current->ItemAttributes->Edition,
“重量”=>($current->ItemAttributes->PackageDimensions->Weight/100),
'Publisher'=>$current->ItemAttributes->Publisher,
'PublishDate'=>$current->ItemAttributes->PublicationDate,
'绑定'=>$current->ItemAttributes->绑定,
“SalesRank”=>$current->SalesRank,
“ListPrice”=>str_replace(“$”,“$current->ItemAttributes->ListPrice->FormattedPrice),
'ImageURL'=>$current->LargeImage->URL,
'DetailURL'=>$current->DetailPageURL
);
}//if语句的结尾
//更新图书表中的标题和生物信息
$conn->查询(“
更新手册
设置isbn13=“$isbn”,
作者=“$amazonResult['author']”,
title=“$amazonResult['title']”,
版本=“$amazonResult['edition']”,
权重=“$amazonResult['weight']”,
publisher=“$amazonResult['publisher']”,
binding='“$amazonResult['binding']”,
上市价格=“$amazonResult['ListPrice']”,
发布日期=“$amazonResult['PublishDate']”
其中isbn13=“$isbn”);
}//每个循环的结束
}
$message=数组('message'=>$sum.'记录已更新');
$conn->close();
echo json_编码($message);
?>
对我来说,一切看起来都是对的,但当我在firebug上运行它时,没有任何消息。我的成功函数中的Console.log(数据)表示空字符串

我做错了什么?我应该为每个循环重新构造我的循环吗


编辑:我更改了部分代码以获得更新记录的准确计数。这是$qtyArray[$x]=$row['quantity']行。My console.log(数据)显示2995条记录已更新,但屏幕上没有显示#消息,只有console.log(数据)。希望这能提供更多的见解。

您的错误可能在while循环中:

while($row = $result->fetch_assoc()) {
        $isbnArray[$x] = $row['isbn13'];
        $sum = array_sum($isbnArray);
} // end of while loop
$x被初始化为0,并且从未更改,因此每次只需覆盖数组中的相同条目

你必须改变:

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

您需要在查询中转义您的

$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");

foreach
是一个单词。如何使用JavaScript访问此代码?Console.log()不在PHP@TusharDhoot的确如此。如此突出的人已经告诉你了。:)正确的字符串连接运算符是
,而不是
+
@afuzzyllama-与其编辑我的代码使其看起来整洁,并对问题投反对票,不如提供一些建设性的信息,我正试图从错误中吸取教训。是吗最好这样做,或者在while循环结束之前放入$x++?@Jim这真的没有什么区别。好的,将其更改为您的建议,仍然没有结果。感谢您查看,总是很高兴有另一组眼睛检查代码,您已经盯了数小时没有问题。您确定您的第一个查询返回结果吗?我在Navicat中运行了它并且得到了2007年的记录当我根据你的建议更改代码时,我得到了一个mysql错误,所以我查找了它,建议我使用“”来代替。我这样做了,我的消息说18500525410454835条记录被更新。我的数据库中甚至没有那么多记录。有什么想法吗?它应该只引用1800-2000条记录。我有更新d原始代码以反映我现在拥有的内容。我认为您应该尝试在代码中一步一步地查找问题所在。首先放置print_r($result);die;这样您就可以看到$results包含的内容,而不运行任何其他内容。如果它看起来正确,则向下移动。print_r($result->fetch_assoc())等。您还可以使用var_dump($results);我在查询之后立即运行print\r($result);die;并获得mysqli\u result对象()。我不明白,该查询在Navicat中运行良好,但不是来自php文件。我并不是说它是查询,只是试图找到问题所在。好的,我们有了对象。现在请继续往下看,看看您需要的内容是否正确。既然您正在执行选择,那么您应该有多行数据,所以请尝试打印($result->fetch_assoc());查看您是否获得了一个数据数组。如果没有,那么下一个问题是您是否有到数据库的有效连接?当我运行print_r($isbnArray);die;在while循环之后,我获得了1884条isbn记录。这是我应该有的,因此我的查询似乎很好。
$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");