最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Inserting more than one array into same row

programmeradmin3浏览0评论

I have a form which can add additional fields by j/s. I am trying to get the values to insert into a single row. Normally you would use a foreach statement. I have tried that for each field and that works but I cannot find how to combine these. There are lots of ways to separate out the arrays but they still appear to need a foreach statement. The form fields look like this

 File Name::&nbsp;<input type="text" class="" name="file_name[]"  />
    File:: &nbsp;<input type="text" name="files[]" value="" />

I have tried a double foreach as follows

if(isset($_POST['file_name'])){
$file_name = $_POST['file_name'] ;//echo $_POST['file_name'];   
}
 if(isset($_POST['files']))
 {$files = $_POST['files'] ;
}
 foreach($file_name as $file_n){
 foreach($files as $file){
$wpdb->insert( 'table', array ('project_ref'=> 
 $reference,'project_name'=>$fil_n,'project_link'=>$file));
}}

Problem is that if you complete two rows of the fields it inserts 4 rows. Very frustrating

I have a form which can add additional fields by j/s. I am trying to get the values to insert into a single row. Normally you would use a foreach statement. I have tried that for each field and that works but I cannot find how to combine these. There are lots of ways to separate out the arrays but they still appear to need a foreach statement. The form fields look like this

 File Name::&nbsp;<input type="text" class="" name="file_name[]"  />
    File:: &nbsp;<input type="text" name="files[]" value="" />

I have tried a double foreach as follows

if(isset($_POST['file_name'])){
$file_name = $_POST['file_name'] ;//echo $_POST['file_name'];   
}
 if(isset($_POST['files']))
 {$files = $_POST['files'] ;
}
 foreach($file_name as $file_n){
 foreach($files as $file){
$wpdb->insert( 'table', array ('project_ref'=> 
 $reference,'project_name'=>$fil_n,'project_link'=>$file));
}}

Problem is that if you complete two rows of the fields it inserts 4 rows. Very frustrating

Share Improve this question edited Feb 17 at 18:03 Ian Young asked Feb 17 at 17:14 Ian YoungIan Young 115 bronze badges 1
  • 1 You should make it easier for those who might be able to help you by indenting and spacing your code correctly and avoiding typos. – migli Commented Feb 17 at 17:33
Add a comment  | 

2 Answers 2

Reset to default 0

foreach loops over a single variable. Here you have two, so using foreach is not the right method. Or you'd have to add a $i index inside the loop to get the second variable. But there's a simpler way:

$total = count($_POST['file_name']);

for($i = 0; $i < $total; $i++) {
    $file_name = $_POST['file_name'][$i];
    $file = $_POST['file'][$i];

    $wpdb->insert( 'table', array ('project_ref'=> $reference,'project_name'=>$file_name,'project_link'=>$file));
}

To be on the safe side, I'll check that the values are present before the wpdb->insert() :

$total = count($_POST['file_name']);

for($i = 0; $i < $total; $i++) {
    $file_name = $_POST['file_name'][$i] ?? '';
    $file = $_POST['file'][$i] ?? '';

    if ( !empty($file_name) && !empty($file) ){
        $wpdb->insert( 'table', [
            'project_ref'=> $reference,
            'project_name'=> $file_name,
            'project_link'=>$file
        ]);
    }
}

I don't know what the $reference variable corresponds to, but I assume it's declared a little higher up in the code.

发布评论

评论列表(0)

  1. 暂无评论