簡體   English   中英

使用多個數組更新mysql表

[英]Update mysql table with multiple arrays

我正在嘗試完成我已經工作了幾天的變化價格更新程序。 我還是很新的 PHP。

我被困在最終的更新順序上。 我沒有收到任何錯誤; 但沒有更新 - 大概是因為 SQL 查詢返回 0 值。

任何幫助都非常感謝。 我確定這很簡單。

期望的結果: _price 在 wp_postmeta 中更新,用於最初檢索的 post_id 記錄和從 post_parent 記錄獲取的 _price。 循環遍歷數組中的所有記錄。

取回數據最終顯示的示例數據:

$postids (721, 735, 749, 807)

$parentids (714, 740, 742, 815)

$價格(11.04、6.32、7.69、21.00)

在理想的世界中,我還想包含一個公式,在插入/更新之前/期間將 _price 乘以 2.1。

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
//Select all POST_IDs for variation 2.1M
$sql = "SELECT post_id FROM wp_postmeta WHERE meta_value = '2-1m'";
$result = $conn->query($sql);
//Array and display POST_IDs
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["post_id"]. "<br>";
    }
} else {
   echo "0 results";
}
//Prepare POST_IDs for next query
$postids = [];
foreach ($result as $row){
    $postids[] = $row["post_id"];
}


//Use POST_IDs to select all PARENT_IDs
if (!empty($postids)) {
$sql2 = "SELECT post_parent FROM wp_posts WHERE ID IN (".implode(',',$postids).")";
}
$result2 = $conn->query($sql2);
//Array and display PARENT_IDs
if ($result2->num_rows > 0) {
    // output data of each row
    while($row2 = $result2->fetch_assoc()) {
        echo "parentid: " . $row2["post_parent"]. "<br>";
    }
} else {
    echo "0 results";
}
//Prepare PARENT_IDs for next query
$parentids = [];
foreach ($result2 as $row2){
    $parentids[] = $row2["post_parent"];
}
//Select PRICES using PARENT_IDs and META_KEY for Price
if (!empty($parentids)) {
$sql3 = "SELECT meta_value FROM wp_postmeta WHERE meta_key = '_price' AND post_id IN (".implode(',', $parentids).")";
}
$result3 = $conn->query($sql3);
if ($result3->num_rows > 0) {
    // output data of each row
    while($row3 = $result3->fetch_assoc()) {
        echo "price: " . $row3["meta_value"]. "<br>";
    }
} else {
    echo "0 results";
}
//Array and display PRICES
$prices = [];
foreach ($result3 as $row3){
    $prices[] = $row3["meta_value"];
}
//Display all retrieved data
echo "<div><p><table><tr><td> ".implode('<br>', $postids)." </td><td> ".implode('<br>', $parentids)." </td><td> ".implode('<br>', $prices)." </td></tr></table></p></div>";
//UPDATE variant prices with parent prices
foreach ($prices as $key => $postids){
    $prices = $row3["meta_value"];
    $postids = $row["post_id"];
$sqlupdate = "UPDATE wp_postmeta SET meta_value = $prices WHERE post_id = $key AND meta_key = '_price'";
$update = $conn->query($sqlupdate);
     if (!$sqlupdate)  {
        echo "error updating $prices" . mysql_error();
     }
}
$conn->close();
?>

您正試圖兩次使用所有查詢結果集。 一旦查詢結果集被while循環消耗,就像到達文件末尾一樣,它就完成了

將您正在使用的 2 個循環中的所有 3 個合並到一個循環中,該循環同時輸出和創建數組。 像這樣

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//Select all POST_IDs for variation 2.1M
$sql = "SELECT post_id FROM wp_postmeta WHERE meta_value = '2-1m'";
$result = $conn->query($sql);

//Array and display POST_IDs

$postids = [];
if ($result->num_rows > 0) {
    // output data of each row
    // and save in array for later use

    while($row = $result->fetch_assoc()) {
        $postids[] = $row["post_id"];
        echo "id: " . $row["post_id"]. "<br>";
    }
} else {
   echo "0 results";
}
/*
No longer needed
//Prepare POST_IDs for next query
foreach ($result as $row){
    $postids[] = $row["post_id"];
}
*/

//Use POST_IDs to select all PARENT_IDs
if (!empty($postids)) {
    $sql2 = "SELECT post_parent 
            FROM wp_posts 
            WHERE ID IN (".implode(',',$postids).")";
}

$result2 = $conn->query($sql2);

//Array and display PARENT_IDs
$parentids = [];
if ($result2->num_rows > 0) {
    // output data of each row
    while($row2 = $result2->fetch_assoc()) {
        $parentids[] = $row2["post_parent"];
        echo "parentid: " . $row2["post_parent"]. "<br>";
    }
} else {
    echo "0 results";
}

//Select PRICES using PARENT_IDs and META_KEY for Price
if (!empty($parentids)) {
    $sql3 = "SELECT meta_value 
                    FROM wp_postmeta 
                    WHERE meta_key = '_price' 
                    AND post_id IN (".implode(',', $parentids).")";
}

$result3 = $conn->query($sql3);

$prices = [];
if ($result3->num_rows > 0) {
    // output data of each row
    while($row3 = $result3->fetch_assoc()) {
        $prices[] = $row3["meta_value"];
        echo "price: " . $row3["meta_value"]. "<br>";
    }
} else {
    echo "0 results";
}

//Display all retrieved data
echo "<div><p><table><tr><td> ".implode('<br>', $postids)." </td><td> ".implode('<br>', $parentids)." </td><td> ".implode('<br>', $prices)." </td></tr></table></p></div>";

在這里,您將覆蓋循環在循環內創建的 $prices 數組。 並使用不再存在的$row3結果集!

恐怕我對數據完全不確定,所以最后一點代碼更改有點猜測。

//UPDATE variant prices with parent prices
foreach ($prices as $key => $postids){

    //$prices = $row3["meta_value"];
    $price = $postids

    //$postids = $row["post_id"];

    $sqlupdate = "UPDATE wp_postmeta 
                    SET meta_value = $price 
                    WHERE post_id = $key 
                    AND meta_key = '_price'";
    $update = $conn->query($sqlupdate);
    if (!$sqlupdate)  {
        echo "error updating $prices" . mysql_error();
    }
}
$conn->close();
?>

這最終是一個簡單的解決方案。 正如我在問題中所說,我是一名 PHP 初學者。 我需要進一步了解 RE 數組。 如果有人感興趣,這是最后的查詢。

//UPDATE variant prices with parent prices
foreach ($prices as $key => $np){
    $pid = $postids["$key"];
    $sqlupdate = "UPDATE wp_postmeta 
                    SET meta_value = $np
                    WHERE post_id = $pid 
                    AND meta_key = '_price'";
    $update = $conn->query($sqlupdate);
    if(mysqli_query($conn, $sqlupdate)){
        echo "Records were updated successfully.";
    } else {
        echo "ERROR: Was not able to execute $sqlupdate. " . mysqli_error($link);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM