<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\'];
}
}
?>
这是我的查询完整代码。
<?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);
}
}
}
?>
当上述代码执行时,wordpress给出以下错误。
Illegal string offset \'timeslot\'
有人能帮忙解决这个问题吗。
最合适的回答,由SO网友:Qaisar Feroz 整理而成
您正在illegal string offset \'timeslot\'
because $time
is not an array, it is an item in array $timeslots
. 您已经检索到使用提交的值$timeslots = $_POST[\'timeslot\'];
您的代码还包含其他键入错误。以下是更正的代码:
<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;
}
}
?>
我希望这有帮助。