我有一个由jquery函数触发的动态下拉菜单。我正在显示mysql db的下拉值。之后,我试图从下拉菜单中插入选定的值。我已经能够通过mysqli_
成功完成,但在使用PDO
时,没有插入任何内容。我仔细检查了变量,没有显示任何错误。如何使用PDO
从下拉菜单中插入所选值? Example
用于创建多个下拉列表的Jquery
<script>
$(document).ready(function () {
$('select').change(function() {
var option = $(this).val();
showFields(option);
return false;
});
function showFields(option){
var content = '';
for (var i = 1; i <= option; i++){
content += '<div id="course_'+i+'"><label>Course # '+i+'</label><br /><label>Course Name:</label> <select id="coursename_'+i+'" name="coursename_'+i+'"><option value="">--- Select ---</option>"'
<?php
$course_query = $db_con->prepare("SELECT course_id, course_name FROM courses_selection_list ");
$course_query->execute();
$data = $course_query->fetchAll();
foreach ($data as $row){
//dropdown values pulled from database
echo 'content += \'<option value="' . $row['course_id'] . ':'.$row['course_name'].'">' . $row['course_name'] . '</option>\';';
}
?>
'"';
content += '</select></br>Class Start Date: <input type="text" id="start_date_'+i+'" name="start_date_'+i+'" class="datepicker" />Class End Date: <input type="text" id="end_date_'+i+'" name="end_date_'+i+'" class="datepicker" /><div>';
}
$('#course_catalog').html(content);
}
});
</script>
PDO插入(不起作用)
<?php
if(isset($_POST['submit'])){
$courses_offered=$_POST['courses_offered'];
$db_con = new PDO($dsn, $user, $password);
for($i=1;$i<=$courses_offered;$i++){
$course_vals = explode(':',$_POST["coursename_".$i]);
$acad_id = $_POST['acad_id'];
$start_date = $_POST["start_date_".$i];
$end_date = $_POST["end_date_".$i];
$query_init3 = "INSERT INTO courses_by_academy (academy_id, course_id, course_name, start_date, end_date) VALUES (:acad_id, :course_vals[0],:course_vals[1],:start_date,:end_date);";
$query_prep3 = $db_con->prepare($query_init3);
$insert_result3 = $query_prep3->execute(array(
"acad_id" => $acad_id,
"course_vals[0]" => $course_vals[0],
"course_vals[1]" => $course_vals[1],
"start_date " => $start_date,
"end_date " => $end_date
));
}
print_r($_POST);
}
?>
Mysqli插入(工作)
<?php
if(isset($_POST['submit'])){
$courses_offered=$_POST['courses_offered'];
$db_con = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
for($i=1;$i<=$courses_offered;$i++)
{
$courseVals=explode(':',$_POST["coursename_".$i]);
$acad_id = $_POST['acad_id'];
$start_date = $_POST["start_date_".$i];
$end_date = $_POST["end_date_".$i];
$db_insert4= $db_con->prepare("INSERT INTO courses_by_academy (academy_id, course_id, course_name, start_date, end_date) VALUES (?,?,?,?,?)");
$db_insert4->bind_param('issss', $_POST['acad_id'], $courseVals[0], $courseVals[1], $start_date, $end_date);
$db_insert4->execute();
}
print_r($_POST);
echo $courseVals;
}
?>
HTML 的
Courses being offered?
<select name="courses_offered">
<option value="default">---Select---</option>
<option value="1">1</option>
</select>
<div id="course_catalog"></div>
<input value="SAVE" name="submit" type="submit">
答案 0 :(得分:2)
如上所述,AdRock请根据您的查询绑定您的值。以下是pdo
样式中for循环的外观:
if(isset($_POST['submit'])){
$courses_offered=$_POST['courses_offered'];
for($i=1;$i<=$courses_offered;$i++){
$course_vals = explode(':',$_POST["coursename_".$i]);
$acad_id = $_POST['acad_id'];
$start_date = $_POST["start_date_".$i];
$end_date = $_POST["end_date_".$i];
$db_insert3= $db_con->prepare("INSERT INTO courses_by_academy (academy_id, course_id, course_name, start_date, end_date) VALUES (?,?,?,?,?)");
$db_insert3->bindParam(1, $acad_id, PDO::PARAM_INT);
$db_insert3->bindParam(2, $course_vals[0], PDO::PARAM_INT);
$db_insert3->bindParam(3, $course_vals[1], PDO::PARAM_INT);
$db_insert3->bindParam(4, $start_date, PDO::PARAM_STR, 12);
$db_insert3->bindParam(5, $end_date, PDO::PARAM_STR, 12);
$db_insert3->execute();
}
print_r($_POST);
}
有关使用bindParam
的进一步说明,请阅读Manual