gpt4 book ai didi

php - WordPress插件激活插入多行

转载 作者:行者123 更新时间:2023-11-29 07:53:47 24 4
gpt4 key购买 nike

我试图在插件激活时将多个原始数据插入数据库,但我设法只插入一个原始数据。
我可以弄清楚如何添加多个原始数据,例如 10 个原始数据。

下面是仅插入一个原始数据的工作代码:

function my_func() {
global $wpdb;
$_IL_TABLE_SETTINGS = $wpdb->prefix . "table";

$wpdb->insert(
$_IL_TABLE_SETTINGS,
array( 'id' => '1', 'options' => 'something', 'values' => 'something' ));
}

我尝试了这样的方法,但对第二个原始数据不起作用:

function my_func() {
global $wpdb;
$_IL_TABLE_SETTINGS = $wpdb->prefix . "table";

$wpdb->insert(
$_IL_TABLE_SETTINGS,
array( 'id' => '1', 'options' => 'something', 'values' => 'something' ),
array( 'id' => '2', 'options' => 'something', 'values' => 'something' )
);
}

我如何将此值添加到您的代码中:

$id = array('1', '2', '3');
$op = array('first', 'second', 'thaird');
$data = array('value1', 'value2', 'value3');

我将不胜感激任何帮助。

最佳答案

        Here is the code which will work.

$id = array('1', '2', '3');
$op = array('first', 'second', 'thaird');
$data = array('value1', 'value2', 'value3');


for($i=0;$i<count($id);$i++)
{
$wpdb->insert($_IL_TABLE_SETTINGS,array("id"=>$id[$i],"option"=>$op[$i],"values"=>$data[$i]));
}

Please let me know if it didnt work for you.

关于php - WordPress插件激活插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25682264/

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