Skip to content

Instantly share code, notes, and snippets.

@julianjupiter
Created December 7, 2015 10:47
Show Gist options
  • Save julianjupiter/16fca4d31e9fb0f5d22d to your computer and use it in GitHub Desktop.
Save julianjupiter/16fca4d31e9fb0f5d22d to your computer and use it in GitHub Desktop.
<?php
try {
$connection = new PDO('mysql:host=localhost;dbname=jtest2', 'root', 'admin123');
} catch (PDOException $e) {
echo $e->getMessage();
}
try {
$querySelect = 'SELECT id, course_type FROM course_type';
$statementSelect = $connection->prepare($querySelect);
$statementSelect->execute();
$data = $statementSelect->fetchAll();
} catch (PDOException $e) {
echo $e->getMessage();
}
$message = '';
if (isset($_POST['btnSave'])) {
if (!empty($_POST['subjectInserting'])) {
$counter = 0;
foreach ($_POST['subjectInserting'] as $subject) {
try {
$queryInsert = 'INSERT INTO student_info(subject_name) VALUES(:subject_name)';
$statementInsert = $connection->prepare($queryInsert);
$result = $statementInsert->execute([':subject_name' => $subject]);
if ($result) {
$counter += 1;
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
$message = $counter . ' subject(s) saved!';
}
}
?>
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post">
<table>
<tr>
<td>
<select name="subjectNames[]" multiple>
<?php
foreach ($data as $d) {
?>
<option value="<?php echo $d['id'];?>"><?php echo $d['course_type'];?></option>
<?php
}
?>
</select>
</td>
<td>
<button type="submit" name="btnTransfer">&gt;</button>
</td>
<td>
<select name="subjectInserting[]" multiple>
<?php
if (isset($_POST['btnTransfer'])) {
foreach ($_POST['subjectNames'] as $selectedSubject) {
?>
<option value="<?php echo $selectedSubject;?>"><?php echo $selectedSubject;?></option>
<?php
}
}
?>
</select>
</td>
<td>
<button type="submit" name="btnSave">Save</button>
</td>
<?php
if ($message != '') {
?>
<td>
<p><?php echo $message;?></p>
</td>
<?php } ?>
</tr>
</table>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment