我试图在插件激活时将多个raw插入数据库,但我只能插入一个raw
我可以想出如何添加多个raw,例如10个raw。
以下是仅插入一个原始代码的工作代码:
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.
例如,如果您想添加10条记录,则可以尝试for
循环:
for($i=0;$i<10;$i++)
{
$wpdb->insert(
$_IL_TABLE_SETTINGS,
array( 'id' => '$i', 'options' => 'something', 'values' => 'something' ),
}
当然,您需要相应地添加动态数据。
您可以在其中添加一个循环,并将$wpdb->insert放入该循环中。
If value is stored in an array
$arr=array();//containing the values you want to insert
$arr[0]=array("id"=>idvalue,"key"=>"value");
for($i=0;$i<count($arr);$i++)
{
$wpdb->insert($_IL_TABLE_SETTINGS,$arr[$i]));
}
if you want to store imaginari values
for($i=0;$i<count($arr);$i++)
{
$wpdb->insert($_IL_TABLE_SETTINGS,array( 'id' => $i, 'options' => 'something', 'values' => 'something' ));
}
Hope you can relate your code with the above one, if no please let me know.