$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - wordpress select multiple options and illegal string offset 'timeslot'|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - wordpress select multiple options and illegal string offset 'timeslot'

matteradmin8PV0评论
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

Improve this question
<form method="POST">
<select name="timeslot[]" multiple="multiple" size = 4 required>
    <option value="1">1</option>
    <option value="1">1</option>
    <option value="1">1</option>
</select>
<input type="submit" name="submit"/>
</form>

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_timeslots = $wpdb->prefix . 'booking_timeslots';   
    $timeslots = $_POST['timeslot'];
       foreach ($timeslots as $time) {
          echo $dd = $time['timeslot'];
       }
}
?>

This is my full code with query.

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_dates = $wpdb->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);
        }
    }
}
?>

While above code executes wordpress gives following error.

Illegal string offset 'timeslot' 

Can someone help to resolve this.

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

Improve this question
<form method="POST">
<select name="timeslot[]" multiple="multiple" size = 4 required>
    <option value="1">1</option>
    <option value="1">1</option>
    <option value="1">1</option>
</select>
<input type="submit" name="submit"/>
</form>

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_timeslots = $wpdb->prefix . 'booking_timeslots';   
    $timeslots = $_POST['timeslot'];
       foreach ($timeslots as $time) {
          echo $dd = $time['timeslot'];
       }
}
?>

This is my full code with query.

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_dates = $wpdb->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);
        }
    }
}
?>

While above code executes wordpress gives following error.

Illegal string offset 'timeslot' 

Can someone help to resolve this.

Share Improve this question edited Mar 5, 2019 at 8:35 Afzal Khan asked Mar 4, 2019 at 11:39 Afzal KhanAfzal Khan 54 bronze badges 3
  • please Elaborate your question. – Gufran Hasan Commented Mar 4, 2019 at 11:40
  • Please change name="timeslot[]" to name="timeslot". – Gufran Hasan Commented Mar 4, 2019 at 11:43
  • Please check by change your select box options values. – Pratik Patel Commented Mar 4, 2019 at 11:49
Add a comment  | 

1 Answer 1

Reset to default 1

You are getting illegal string offset 'timeslot' because $time is not an array, it is an item in array $timeslots. You have already retrieved the values submitted using $timeslots = $_POST['timeslot'];

Your code also contains other typing mistakes. Here is corrected code:

<form method="POST">
   <select name="timeslot[]" multiple="multiple" size = 4 required>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
   </select>
   <input type="submit" name="submit"/>
</form>

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_timeslots = $wpdb->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;
    }
}
?>

I hope this helps.

Post a comment

comment list (0)

  1. No comments so far