简体   繁体   中英

How Do I fetch multiple rows from MySQL and and make them the value of checkbox(es) to be inserted to a another table in the database, in php?

I have a table named subjects and another named classes. The design of the app am building is such that the user first fills a form to send subjects offered in a school to my table "subjects". Then he fills another form to create classes in the school, selecting what subjects (from those is table "subjects") are offered in the new class being created. On form submission, their inputs go to a table "classes" which has the following rows; id, className, classSubjects.

Here is my code:

<?php require_once 'controller/connect.php'; ?>

<!DOCTYPE html>
<html><head><title>TTApp</title></head>
<body>

<form action="controller/classReg.php" method="post">
<label>Class Name</label>
<input type="text" name="className" required="true"><br><br>
<label>Subjects Offered</label>

        <?php
        $sql = "SELECT * FROM subjects";
        $result = $connect->query($sql);

        if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                echo "<input type='checkbox' name='classSubjects' value=".$row['title'].">
                   ".$row['title']." ";
            }
        } 
        ?>
    <br><br><input type="submit" name="submit" value="Register Class">
</form>
</div>

</body>
</html>

And the controller/classReg.php here:

<?php 
    require_once 'connect.php';

    if($_POST) {
        if(isset($_POST['className'])) {
            $className= $_POST['className'];   
        }

        if(isset($_POST['classSubjects'])) {
            $classSubjects  = array($_POST['classSubjects']); 
            echo $classSubjects;
        } else {
            echo "no class subject";
        }

        $sql = "INSERT INTO classes (className, classSubjects) VALUES ('$className', '$classSubjects')";
        if($connect->query($sql) === TRUE) {
            echo "<p>New Record Successfully Created</p>";
            echo "<a href='../create.php'><button type='button'>Back</button></a>";
            echo "<a href='../index.php'><button type='button'>Home</button></a>";
        } else {
            echo "Error " . $sql . ' ' . $connect->connect_error;
        }

        $connect->close();
    }

?>

This isn't working. is correctly displayed loading all subjects in table "subjects" but only the last subject gets to the table "classes". Am feeling I need to use arrays somewhere but stuck.

First change this line :

echo "<input type='checkbox' name='classSubjects' value=".$row['title'].">

To:

echo "<input type='checkbox' name='classSubjects[]' value=".$row['title'].">

Then you r controller/classReg.php here:

<?php
require_once 'connect.php';

    if ($_POST) {
        if (isset($_POST['className'])) {
            $className = $_POST['className']; 
        }


        if (isset($_POST['classSubjects'])) {
            $classSubjects = $_POST['classSubjects'];
        }else {
            echo "no class subject";
        }

        $sql  = "INSERT INTO classes (className, classSubjects) VALUES (?, ?)";
        $stmt = $connect->prepare($sql);

        foreach ($classSubjects as $key => $subject) {
            $stmt->bind_param("ss", $className, $subject);
        }
        if ($stmt->execute()) {

            echo "<p>New Record Successfully Created</p>";
            echo "<a href='../create.php'><button type='button'>Back</button></a>";
            echo "<a href='../index.php'><button type='button'>Home</button></a>";
        } else {

            echo "Error " . $connect->error;

        }

        $stmt->close();
        $connect->close();
    }

    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM