首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wordpress选择多个选项和非法字符串偏移“timeslot”

wordpress选择多个选项和非法字符串偏移“timeslot”
EN

WordPress Development用户
提问于 2019-03-04 11:39:13
回答 1查看 104关注 0票数 -1
代码语言:javascript
复制
    1
    1
    1




prefix . 'booking_timeslots';   
    $timeslots = $_POST['timeslot'];
       foreach ($timeslots as $time) {
          echo $dd = $time['timeslot'];
       }
}
?>

这是我的完整查询代码。

代码语言:javascript
复制
prefix . 'booking_dates';   //'booking_dates' is a table name which is in the database
    $booking_timeslots = $wpdb->prefix . 'booking_timeslots';   //'booking_timeslots' is a table name which is in the database

    $year = $_POST['year'];
    $month = $_POST['month'];
    $days = $_POST['days'];
    $timeslots = $_POST['timeslot'];

        $data['year'] = $year;      // $data['year'] year is a name of column in database
        $data['month'] = $month;
    foreach ($days as $day) {           // $data['month'] month is a name of column in database
        $data['day'] = $day;            // $data['day'] day is a name of column in database

        $wpdb->insert($booking_dates,$data);
        $last_record_id = $wpdb->insert_id;

        foreach ($timeslots as $timeslot) {
            $data_timeslot['bid'] = $last_record_id;
            $data_timeslot['time'] = $timeslot;
            $wpdb->insert($booking_timeslots,$data_timeslot);
        }
    }
}
?>

当上面的代码执行时,wordpress会给出以下错误。

代码语言:javascript
复制
Illegal string offset 'timeslot' 

有人能帮我解决这个问题吗。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-03-04 12:27:27

获得illegal string offset 'timeslot' 是因为 $time is不是数组,而是数组 $timeslots中的一个项。您已经检索了使用$timeslots = $_POST['timeslot'];提交的值。

您的代码还包含其他类型错误。以下是更正后的代码:

代码语言:javascript
复制
      1
      2
      3
   
   


prefix . 'booking_timeslots';   
    $timeslots = $_POST['timeslot'];
    // you can use print_r() here 
    print_r( $timeslots );  // to see what is submitted from form

    foreach ($timeslots as $time) {
          echo $time;
    }
}
?>

我希望这能帮到你。

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/330598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档