gpt4 book ai didi

php - WordPress $wpdb。插入多条记录

转载 作者:IT老高 更新时间:2023-10-28 23:43:12 25 4
gpt4 key购买 nike

有没有办法在 Wordpress 中使用 $wpdb->insert

完成以下操作

$wpdb->query($wpdb->prepare)):

INSERT into TABLE (column1, column2, column3) 
VALUES
('value1', 'value2', 'value3'),
('otherval1', 'otherval2', 'otherval3'),
('anotherval1', 'anotherval2', 'anotherval3')

...等

最佳答案

好的,我想通了!

为实际值和占位符设置数组

$values = array();
$place_holders = array();

初始查询:

$query = "INSERT INTO orders (order_id, product_id, quantity) VALUES ";

然后循环遍历您要添加的值,并将它们插入到适当的数组中:

foreach ( $_POST as $key => $value ) {
array_push( $values, $value, $order_id );
$place_holders[] = "('%d', '%d')" /* In my case, i know they will always be integers */
}

然后将这些位添加到初始查询中:

$query .= implode( ', ', $place_holders );
$wpdb->query( $wpdb->prepare( "$query ", $values ) );

关于php - WordPress $wpdb。插入多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12373903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com