FIRST CHAT GPT CODE

mail@pastecode.io avatar
unknown
php
2 years ago
2.4 kB
2
Indexable
<?php
    require 'connect_to_database.php';
    $subject_id = $subject_id_err = "";
    $sql1 = "Select * FROM tbl_subject";
	$result4 = mysqli_query($conn, $sql1);
	$options4 = "<option>Select a Subject</option>";
	while ($row = mysqli_fetch_array($result4)) {
		$options4 = $options4 . "<option value='$row[0]'>$row[2]</option>";
	}

	if(isset($_POST['ins']))
	{
		
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        $input_subject_id = trim($_POST["subject_id"]);
		if($input_subject_id == "Select a Subject") {
			$subject_id_err = "Please Select a Subject.";
		} else{
			$subject_id = (int)$input_subject_id;
		}
        $n = $_POST['n'];
        for($i=1;$i<=$n;$i++)
        {
            $sql = "INSERT INTO tbl_schedgrade (schedule) VALUES (?)";
            if ($stmt = mysqli_prepare($conn, $sql)){
                mysqli_stmt_bind_param($stmt, "s", $_POST['subject_id_'.$i]);
                if(mysqli_stmt_execute($stmt)){
                    // Insert successful
                } else{
                    // Insert failed
                    echo "Oops!<br>Something went wrong.<br>Please try again later.";
                }
                mysqli_stmt_close($stmt);
            }
        }
        mysqli_close($conn);
    }
}
?>
<!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">
<form action="" method="post">
<?php   
    $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="subject_id_<?php echo $i; ?>" class="form-control <?php echo (!empty($subject_id_err)) ? 'is-invalid': ''; ?>">
									<?php echo $options4; ?>
								</select>
								<span class="invalid-feedback"><?php echo $subject_id_err;?></span>
							</div>

    <?php
    }
?>
    <input type="submit" name="ins" value=" SAVE ">
    </form>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>