我有一个带有多个选择的下拉框。下拉列表如下所示:
<select multiple class="form-control" name="batch_no[]" id="batch_no" required onchange="getBatchCourseDetail();">
<option value="">-----------Select Your Batch----------</option>
<?php
foreach ($result as $res)
{
?>
<option value="<?php echo $res['batch_code']; ?>"><?php echo $res['batch_code']; ?></option>
<?php
} ?>
</select>
在onchange函数调用中我有这样的脚本:
function getBatchCourseDetail()
{
var other = String($('#batch_no').val());
var opts = [],
opt;
var split1 = other.split(',');
for (var i = 0; i < split1.length; i++)
{
opt = split1[i];
opts.push(opt);
}
$.ajax({
url: 'course_apply_batch_course_detail_ajax.php',
type: 'POST',
data:
{
batch_code: opts
},
success: function (data)
{
//console.log(data);
$('#batch_information_autofill').html(data);
}
});
}
在course_apply_course_detail_ajax.php页面的ajax文件中,我检索下拉列表的所有值。
course_apply_course_detail_ajax.php:
<?php
require('classes/autoloader.php');
$course_apply = new \Model\CourseApplyModel();
$batch_code111=array();
$batch_code111 = $_POST['batch_code'];
$batch_code1 ="'" .implode("','",$batch_code111) ."'";
$parameter = array(
"batchcode" => $batch_code1,
"status" => 0);
$result11 =$course_apply->getBatchCourseDetail11($parameter);
echo"<pre>";
print_r($result11);
echo"</pre>";
exit;
?>
我将获得$ batch_code1值:&#39; LATS-CHMB-1000&#39; LATS-SA-1000&#39; LATS-ABSE-1003&#39; LATS-ABSE-1003&#39 ;它存储在数组变量&#39; batchcode&#39;。
中在模型中我的查询如下所示:
public function getBatchCourseDetail11($parameter)
{
/* $QUERY1="SELECT start_date,end_date,course_code FROM batch WHERE
batch_code IN('LATS-CHMB-1000','LATS-SA-1000','LATS-ABSE-1003') AND status =0"; */
$query1="SELECT start_date,end_date,course_code FROM batch WHERE status =:status AND batch_code IN(:batchcode)";
try{
$result1=$this->dbh->prepare($query1);
$result1->execute($parameter);
$data11=$result1->fetchAll(\PDO::FETCH_ASSOC);
return $data11;
}
catch(\PDOException $e)
{
print_r($e);
return false;
}
}
当我尝试打印返回数据时,我得到了array(),但查询在phpmyadmin中正确执行。
先谢谢,请帮我解决这个问题。
答案 0 :(得分:0)
查询是您的问题
$query1="SELECT start_date,end_date,course_code
FROM batch
WHERE status =:status
AND batch_code IN(:batchcode)";
无法用占位符替换任意查询部分。因此,对于逗号分隔的占位符,如IN(),您必须手动创建一组?并将它们放入查询中:
简而言之,如果您在IN(?,?,?)
列表中有3个值,则必须创建类似IN()
的语法,然后将3个参数传递给准备。
public function getBatchCourseDetail11($parameter)
{
// generate the number of ? we need
$ins = str_repeat('?,', count($parameter['batchcode']) - 1) . '?';
$query1="SELECT start_date,end_date,course_code
FROM batch
WHERE status = ?
AND batch_code IN($ins)";
try{
$result1=$this->dbh->prepare($query1);
$params[] = $parameter['status'];
foreach ( $parameter['batchcode'] as $p ) {
$params[] = $p;
}
$result1->execute($params);
$data11=$result1->fetchAll(\PDO::FETCH_ASSOC);
return $data11;
}
catch(\PDOException $e) {
print_r($e);
return false;
}
}