如何从此代码为每一行更新MySQL?

时间:2019-05-15 17:15:02

标签: php mysql

我正在尝试更新“跟踪”,“运营商”,“状态”行,其中状态为“等待跟踪”

$sql = "SELECT * FROM orders WHERE status = 'Waiting for tracking'";
$result = $mysqli->query($sql);

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $id = $row['id'];
        $tracking = $row['tracking'];
        $carrier = $row['carrier'];
        $link = $row['link'];

        // get DOM from URL or file
        $html = file_get_html($link);

        // find td tags with data-qa=shipment-tracking-number
        foreach($html->find('td[data-qa=shipment-tracking-number]') as $tracking)
            $tracking = $tracking->plaintext;

        // find td tags with data-qa=shipment-carrier
        foreach($html->find('td[data-qa=shipment-carrier]') as $carrier)
            $carrier = $carrier->plaintext;

        if(isset($tracking)) {
            $tracking = "UPDATE orders SET tracking = '$tracking' WHERE id = '$id'";
            $tracking_result = $mysqli->query($tracking);

            if($mysqli->query($tracking_result) === true) {
                $pendnig = "UPDATE orders SET status = 'Pending' WHERE id = '$id'";
                $pending_result = $mysqli->query($pending);
            }
        }

        if(isset($carrier)) {
            $carrier = "UPDATE orders SET carrier = '$carrier' WHERE id = '$id'";
            $carrier_result = $mysqli->query($carrier);
        }
    }
}

执行文件时,仅将“跟踪”和“运营商”的更新状态更新为“待处理”,并且仅将第一行的更新状态更新为其他状态。

2 个答案:

答案 0 :(得分:0)

您可以检查更新查询是否成功,如下所示:

if($tracking_result) {
    $pendnig = "UPDATE orders SET status = 'Pending' WHERE id = '$id'";
    $pending_result = $mysqli->query($pending);
}

希望它对您有帮助。

答案 1 :(得分:0)

我已经用新代码修复了它。

$sql = "SELECT * FROM orders WHERE status = 'Waiting for tracking'";
$result = $mysqli->query($sql);

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $id = $row['id'];
        $tracking = $row['tracking'];
        $carrier = $row['carrier'];
        $link = $row['link'];

        // get DOM from URL or file
        $html = file_get_html($link);

        // find td tags with data-qa=shipment-tracking-number
        foreach($html->find('td[data-qa=shipment-tracking-number]') as $tracking)
            $tracking = $tracking->plaintext;

        // find td tags with data-qa=shipment-carrier
        foreach($html->find('td[data-qa=shipment-carrier]') as $carrier)
            $carrier = $carrier->plaintext;

        if(isset($tracking)) {
            $tracking = "UPDATE orders SET tracking = '$tracking' WHERE id = '$id'";
            $tracking_result = $mysqli->query($tracking);

            if($tracking_result === true) {
                $pendnig = "UPDATE orders SET status = 'Pending' WHERE id = '$id'";
                $pending_result = $mysqli->query($pendnig);
            }
        }

        if(isset($carrier)) {
            $carrier = "UPDATE orders SET carrier = '$carrier' WHERE id = '$id'";
            $carrier_result = $mysqli->query($carrier);
        }

        // clean up memory
        $html->clear();
        unset($html);
    }
}