我有一个PHP文件,可以达到某个点,但我需要一些帮助在循环上做一个MySQl插入。
以下执行SELECT,然后存储order_status为'S'的所有记录的订单ID。这完美地工作,打印每个适当的订单ID。
然后我将那些受影响的ORder ID推送到一个数组,以便我可以保存它们以用于各种功能。
//Query for checking all records in order_status
$orderCheck = "
SELECT
order_id,
order_status
FROM order_status
";
$result = mysqli_query($mysqlConn, $orderCheck);
$order_ids = array();
//loop results to gather order IDs and store them
while ($row = mysqli_fetch_array($result))
{
$order_id = $row['order_id'];
if ($row['order_status'] == "S")
{
array_push($order_ids, $order_id);
}
}
//print_r($order_ids);
//This does indeed print the correct order Ids
以下部分需要一些工作,我不太清楚这里要做什么。我正在迭代和预测那些迭代我有更新,但我担心更新的语法不正确,我不知道我是否需要在这里做另一个数组。
//This is where I'm stuck//
//Now I need to iterate through those orderIDs and update is_placement to 1, since it's a bit column, and udpate date_updated to curdate()
for($i=0; $i<count($order_id); $i++) {
$sql = "UPDATE order_status SET is_placement = '1' and date_updated = curdate() WHERE order_id = '$order_id'"; //I don't believe this syntax is correct
}
基本上,我需要为先前存储的$ order id数组的每次迭代执行mysql更新。
任何想法或建议/指导都非常感谢。
更新:
数组的输出:
[287] => 11605809
[288] => 11605817
[289] => 11605825
[290] => 11605863
[291] => 11605869
[292] => 11605870
[293] => 11605875
[294] => 12605471
[295] => 12605643
[296] => 12605715
[297] => 12605778
[298] => 12605817
向MySQL询问所有订单是没有意义的,然后手动选择状态为S.
更好:
// Select only orders in status S.
$orderCheck = "SELECT order_id FROM order_status WHERE order_status = 'S';"
$result = mysqli_query($mysqlConn, $orderCheck);
$order_ids = array();
while ($row = mysqli_fetch_array($result)) {
$order_ids[] = $row['order_id'];
}
现在您要更新这些记录。如果你一个接一个地做,你可以:
foreach ($order_ids as $order_id) {
$sql = "UPDATE order_status SET is_placement = 1, date_updated = DATE(NOW()) WHERE order_id = '$order_id';";
// EXECUTE
}
更快的方法是(我不添加引号,假设订单ID是数字,但如果它们不是事情变得有点复杂)将所有订单ID放在一起并使用IN
语法:
$bunchOfOrders = implode(',', $order_ids);
$singleQuery = "UPDATE order_status SET is_placement = 1, date_updated = DATE(NOW()) WHERE order_id IN ({$bunchOfOrders})";
但你也可以在MySQL中完成所有这些:
$orderUpdate = "UPDATE order_status SET is_placement = 1, date_updated = NOW() WHERE order_status = 'S';"
$result = mysqli_query($mysqlConn, $orderUpdate);
由于上述内容不会从S更改订单状态,因此您还可以运行$ orderCheck并检索所涉及的订单ID(如有必要)。
为了确保不会发生任何不幸事件,我还会将整个操作包装在事务中。
如果我们必须根据某些条件运行一系列不同的修改(当然是互斥的),该怎么办?
我们可以用地图做这个,只要我们使用确定性函数并且没有修改就会改变下面的条件(所以,你不能在任何“食谱”中改变order_status,否则它会影响后面的那些):
$todo = array(
"order_status='S'" => "is_placement = 1, date_updated = DATE(NOW())",
"order_status='K'" => "is_placement = 2, date_updated = DATE(NOW())",
"order_status='L'" => "owner='NOBODY'",
"o_s='X' AND a=12" => "note='A random condition'",
// We can also supply a "what to do if none of the above":
"DEFAULT" => "note='THIS RECORD DOES NOT MATCH'",
);
$else = array();
foreach ($todo as $when => $then) {
if ('DEFAULT' !== $when) {
$else[] = "({$when})";
} else {
// "ELSE" means when no other condition is matched.
// i.e. NOT when1 AND NOT when2 AND NOT when3...
// which is equivalent to NOT ( when1 OR when2 ... )
$when = "NOT (" . implode(' OR ', $else) . ")";
}
$sql = "UPDATE order_status SET {$then} WHERE {$when};";
// Execute $sql.
}
我会更改初始查询,因此您不必多次循环任何内容。
试试这个:
//Query for checking all records in order_status
$orderCheck = "
SELECT
order_id,
order_status
FROM order_status
WHERE order_status = 'S'
";
$result = mysqli_query($mysqlConn, $orderCheck);
$order_ids = array();
//loop results to gather order IDs and store them
while ($row = mysqli_fetch_row($result))
{
$sql = "UPDATE order_status SET is_placement = '1' and date_updated = curdate() WHERE order_id = '$row[0]'";
}