dimanche 18 janvier 2015

How to insert multiple rows by select or checkbox

I've got a problem with inserting multiple row to one table.


I've got a 3 tables: 1. student with id_student 2. ankieta with id_ankieta 3. student_ankieta with id_student and id_ankieta


I want to choose students from database using select or checkbox and choose one id_ankieta. After confirming, there are rows created in table (student_ankieta). Now I can choose students but when I confirm, only one student gets added to the database.


Can anyone help me corect the code?



<?php
echo'<form method="post" action="student_ankieta.php">
<div class="box" style="margin:0 auto; top:0px;">
<h1>Student - ankieta:</h1>

<label><span><br/>Ankieta:</span></label>
<select class="wpis" name="id_ankieta">';
echo'<option>wybierz ankiete</option>';
$query = "SELECT * FROM ankieta";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo '<option value="'.$row['id_ankieta'].'">' . $row{'rok_akademicki'}.' '. $row{'semestr_akademicki'}.' '.$row{'active_ankieta'} .'</option>';
}
echo '</select>';
$query = "SELECT * FROM student";
$result = mysql_query($query);
echo'<label><span><br/>Wybierz stundentów:</span></label><select multiple="multiple" name="id_student[]" size="10">
';
while ($row = mysql_fetch_assoc($result)) {
echo '<option class="wpis" value="'.$row['id_student'].'" />'.$row{'pesel'}.' '. $row{'nazwisko'}.' '.$row{'imie'} .'</option>';
}
echo'<br/><input class="button" type="submit" value="Dodaj ankiete" name="dodaja">
';

if(isset($_POST['dodaja'])) {
$id_ankieta = $_POST['id_ankieta'];
if(empty($_POST['id_ankieta'])) {
echo '<p style="margin-top:10px; font-size:75%; font-family: Calibri; color: red; text-align:center;">Musisz wypełnić wszystkie pola.</p>';
}
else{
$id_student = $_POST['id_student'];
for ($i = 0; $i < count($id_student); $i++) {
$id_student = $id_student[$i];
mysql_query("INSERT INTO student_ankieta (id_student, id_ankieta) VALUES ('" . $id_student . "','$id_ankieta')");
}}}
echo'</div></form>';?>

Aucun commentaire:

Enregistrer un commentaire