У меня есть два динамических мультиселектора полей формы, и я пытаюсь вставить все записи из формы в базу данных одновременно.Однажды селектор предназначен для района, а другой - список элементов данных.Для каждого района должна быть запись для каждого элемента данных.Таким образом, если кто-то выбирает 2 района и 3 элемента данных, в базу данных следует вставить 6 записей.Я ценю любую помощь, которую может оказать каждый.Спасибо.
Вот мой код формы:
<form method="post" action="process.php" enctype="multipart/form-
data">
<div class="row">
<div class="col-xs-5">
<select name="from[]" id="search" class="form-control" size="15"
multiple="multiple">
<?php
while(!$category->atEnd()) { //dyn select
?>
<option value="<?php echo($category-
>getColumnVal("data_elementID")); ?>"><?php echo($category-
>getColumnVal("element_name")); ?></option>
<?php
$category->moveNext();
} //dyn select
$category->moveFirst();
?>
</select>
</div>
<div class="col-xs-2">
<button type="button" id="search_rightAll" class="btn btn-block"><i
class="glyphicon glyphicon-forward"></i></button>
<button type="button" id="search_rightSelected" class="btn btn-
block"><i class="glyphicon glyphicon-chevron-right"></i></button>
<button type="button" id="search_leftSelected" class="btn btn-
block"><i class="glyphicon glyphicon-chevron-left"></i></button>
<button type="button" id="search_leftAll" class="btn btn-block"><i
class="glyphicon glyphicon-backward"></i></button>
</div>
<div class="col-xs-5">
<select name="data_elementID[]" id="search_to" class="form-control"
size="15" multiple="multiple">
</select>
</div>
</div>
<p> </p>
<h4>Please select the district(s) that use this resource that you
would like to assign these data elements to:</h4>
<div class="row">
<div class="col-xs-5">
<select name="from[]" id="search2" class="form-control" size="15"
multiple="multiple">
<?php
while(!$districts_selected->atEnd()) { //dyn select
?>
<option value="<?php echo($districts_selected-
>getColumnVal("districtID")); ?>"><?php echo($districts_selected-
>getColumnVal("district_name")); ?></option>
<?php
$districts_selected->moveNext();
} //dyn select
$districts_selected->moveFirst();
?>
</select>
</div>
<div class="col-xs-2">
<button type="button" id="search2_rightAll" class="btn btn-block"><i
class="glyphicon glyphicon-forward"></i></button>
<button type="button" id="search2_rightSelected" class="btn btn-
block"><i class="glyphicon glyphicon-chevron-right"></i></button>
<button type="button" id="search2_leftSelected" class="btn btn-
block"><i class="glyphicon glyphicon-chevron-left"></i></button>
<button type="button" id="search2_leftAll" class="btn btn-block"><i
class="glyphicon glyphicon-backward"></i></button>
</div>
<div class="col-xs-5">
<select name="districtID[]" id="search2_to" class="form-control"
size="15" multiple="multiple">
</select>
</div>
</div>
<?php
$wa_startindex = 0;
while(!$category->atEnd()) {
$wa_startindex = $category->Index;
?>
<input name="softwareID[]" type="hidden" multiple="multiple" value="
<?php echo $_GET['softwareID']; ?>">
<?php
$category->moveNext();
}
$category->moveFirst(); //return RS to first record
unset($wa_startindex);
unset($wa_repeatcount);
?>
<br><br>
<input type="image" src="images/save.png" name="submit" id="submit"
alt="Save" />
</form>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#search').multiselect({
search: {
left: '<input type="text" name="q" class="form-control"
placeholder="Search..." />',
right: '<input type="text" name="q" class="form-control"
placeholder="Search..." />',
},
fireSearch: function(value) {
return value.length > 3;
}
});
});
</script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#search2').multiselect({
search: {
left: '<input type="text" name="q" class="form-control"
placeholder="Search..." />',
right: '<input type="text" name="q" class="form-control"
placeholder="Search..." />',
},
fireSearch: function(value) {
return value.length > 3;
}
});
});
</script>
Вот мой код вставки:
<?php
$dbh = new PDO("mysql:host=$hostname;dbname=mspa",$user,$pass);
?>
<?php
$array =
array($_POST['districtID'], $_POST['softwareID'],
$_POST['data_elementID']);
// pdo example
$sql = 'INSERT INTO district_data_elements (districtID, softwareID,
data_elementID) VALUES (:value1, :value2, :value3)';
// $dbh is pdo connection
$insertTable = $dbh->prepare($sql);
$countArray = count($array);
$i = 0;
while ($i < $countArray) {
$insertTable->bindParam(':value1', $array[1][$i], PDO::PARAM_INT);
// if value is int
$insertTable->bindParam(':value2', $array[2][$i], PDO::PARAM_STR);
// if value is str
$insertTable->bindParam(':value3', $array[3][$i], PDO::PARAM_STR);
$insertTable->execute();
$i++;
}
?>