CODE

mail@pastecode.io avatar
unknown
php
a year ago
4.5 kB
2
Indexable
Never
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
		<style>
			.wrapper{
				width: 600px;
				margin: 0 auto;
			}
		</style>
	</head>
	<body>
		<div class="wrapper">
			<div class="container-fluid">
				<div class="row">
					<div class="col-md-12">
                        <?php
                        require 'connect_to_database.php';
                        $tbl_schedule_id = $tbl_schedule_id_err = "";
                        $sql1 = "SELECT * FROM tbl_schedule";
                        $result4 = mysqli_query($conn, $sql1);
                        $options4 = "<option>Select a Schedule</option>";
                        while ($row = mysqli_fetch_array($result4)) {
                            $options4 .= "<option value='$row[0]'>$row[1]</option>";
                        }
                        if (isset($_GET["student_id"]) && !empty(trim($_GET["student_id"]))) {
                            if (isset($_POST['ins'])) {
                                $input_tbl_schedule_id = isset($_POST["tbl_schedule_id"]) ? trim($_POST["tbl_schedule_id"]) : "";
                                if (empty($input_tbl_schedule_id)) {
                                    $tbl_schedule_id_err = "Please select a Schedule.";
                                } else {
                                    $tbl_schedule_id = (int) $input_tbl_schedule_id;
                                }
                                $n = $_POST['n'];
                                for ($i = 1; $i <= $n; $i++) {
                                    $sql = "INSERT INTO testing (student_id, tbl_schedule_id) VALUES (?, ?)";
                                    if ($stmt = mysqli_prepare($conn, $sql)) {
                                        $param_student_id = trim($_GET["student_id"]);
                                        mysqli_stmt_bind_param($stmt, "ii", $param_student_id, $_POST['tbl_schedule_id_' . $i]);
                                        if (mysqli_stmt_execute($stmt)) {
                                            // Insert successful
                                            echo "Inserted row " . $i . " successfully<br>";
                                        } else {
                                            // Insert failed
                                            echo "Oops!<br>Something went wrong.<br>Please try again later.";
                                        }
                                        mysqli_stmt_close($stmt);
                                    }
                                }
                                mysqli_close($conn);
                            }
                        }
                        ?>
                        <form action="" method="post">
                            How many schedules will you add? <input type="number" name="n" value="">
                            <input type="submit" name="s" value=" GO "><br>
                            <?php
                            if (isset($_POST['s'])) {
                                $n = $_POST['n'];
                                for ($i = 1; $i <= $n; $i++) {
                                    ?>
                                    <input type="hidden" name="n" value="<?php echo $n; ?>">
                                    <br><br><br>
                                    <div class="form-group">
                                        <label>Subject Code #<?php echo $i; ?>: </label>
                                        <select name="tbl_schedule_id_<?php echo $i; ?>" class="form-control <?php echo (!empty($tbl_schedule_id_err)) ? 'is-invalid': ''; ?>">
                                            <?php echo $options4; ?>
                                        </select>
                                        <span class="invalid-feedback"><?php echo $tbl_schedule_id_err;?></span>
                                    </div>
                                    <?php
                                }
                                ?>
                                <input type="submit" name="ins" value=" SAVE ">
                            <?php
                            }
                            ?>
                        </form>

					</div>
				</div>
			</div>
		</div>
	</body>
</html>