[英]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.