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.
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.
name="timeslot[]"
toname="timeslot"
. – Gufran Hasan Commented Mar 4, 2019 at 11:43