我在将此表格提交到数据库表时遇到问题

时间:2019-06-16 09:52:03

标签: php html mysqli

如果我从html表单中选择2个席位,则提交按钮将起作用,因为它将显示所有表单字段,但是当我选择1时,提交表单将不起作用。我是PHP新手

我使用此javascript隐藏/显示了基于选择字段的一些输入字段。

function showDiv(select) {
  if (select.value == 2) {
    document.getElementById('div2a').style.display = "block";
  } else {
    document.getElementById('div2a').style.display = "none";
  }
}
<style>.form2a {
  display: none;
}

</style>
<form name="register" method="POST" action="process.php">
    <label>Select Number of Sitting(s)</label>
    <select name="sitting" id="sitting" class="form-control" onchange="showDiv(this)">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>

    <div class="output1" id="div1" name="onesitting" style="padding-bottom:15%;">
        <label>School Name</label>
        <input type="text" name="schoolname[]" id="schoolname1" class="form-control" placeholder="School Name" required>
        <label>Exam Type</label>
        <select name="examboard[]" id="examboard1" class="form-control" required>
            <option value="">Exam Type</option>
            <option value="WAEC">WAEC(SSCE)</option>
            <option value="WAEC">WAEC(GCE)</option>
        </select>
    </div>
    <div class="form2a" id="div2a" name="twositting">
        <h4
            style="text-align:center; margin-top: 10px; margin-bottom: 5%; background-color:#0d4115; width: auto; color: white;">
            SECOND SITTING</h4>
        <label>School Name</label>
        <input type="text" name="schoolname[]" id="schoolname2" class="form-control" placeholder="School Name" required>
        <label>Exam Type</label>
        <select name="examboard[]" id="examboard2" class="form-control" required>
            <option value="">Exam Type</option>
            <option value="WAEC">WAEC(SSCE)</option>
            <option value="WAEC">WAEC(GCE)</option>
        </select>
    </div>
    <table width="100%" cellpadding="0" cellspacing="0" border="0" class="table table-borderless" id="example">
        <tr>
            <td width="12%"><label class="control-label">S/NO</label></td>
            <td width="53%"><label class="control-label">SUBJECTS</label></td>
            <td width="35%"><label class="control-label">GRADE</label></td>
        </tr>
        <tr>
            <td>1</td>
            <td>
                <?php include("config.php");
                    $sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
                    $query = mysqli_query($conn, $sql);
                    $count  = mysqli_num_rows($query);
                 ?>
                <select name="subjects[]" class="form-control" id="subject">
                    <option value="" selected="selected">Select subject</option>
                    <?php 
                        if($count > 0){
                            while($row = mysqli_fetch_assoc($query)){
                            $subject_id = $row['subject_id'];
                            $subject_name = $row['subject_name'];
                            echo "<option>" . $row['subject_name'] . "</option>";
                            }
                        }else{
                            echo "<option value = ''>Subject not available</option>";
                        }
                    ?>
                </select>
            </td>
            <td>
                <select name="grades[]" class="form-control">
                    <option value=""> Select</option>
                    <option value="A1">A1</option>
                    <option value="B2">B2</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>2</td>
            <td>
                <?php include("config.php");
                    $sql = "SELECT * FROM subjects ORDER BY subject_name ASC";
                    $query = mysqli_query($conn, $sql);
                    $count  = mysqli_num_rows($query);
                ?>
                <select name="subjects[]" class="form-control" id="subject">
                    <option value="" selected="selected">Select subject</option>
                    <?php 
                        if($count > 0){
                            while($row = mysqli_fetch_assoc($query)){
                            $subject_id = $row['subject_id'];
                            $subject_name = $row['subject_name'];
                            echo "<option>" . $row['subject_name'] . "</option>";
                            }
                        }else{
                            echo "<option value = ''>Subject not available</option>";
                        }
                    ?>
                </select>
            </td>
            <td>
                <select name="grades[]" class="form-control">
                    <option value=""> Select </option>
                    <option value="A1">A1</option>
                    <option value="B2">B2</option>
                </select>
            </td>
        </tr>
        <tr>
    </table>
    <br>
    <input type="submit" name="submit" value="submit">
</form>

这是我处理此表单的php代码。选择了2个席位但选择一个席位时,我可以提交此表单。

  <?php
    $sitting = $_POST['sitting'];
    $schoolname = implode("," , $_POST['schoolname']);
    $examboard = implode("," , $_POST['examboard']);
    $examdate = implode("," , $_POST['examdate']);
    $examno = implode("," , $_POST['examno']);
    $subjects = implode("," , $_POST['subjects']);
    $grades = implode("," , $_POST['grades']);
    if(isset($_POST['submit'])){
        $stmt = $conn->prepare("INSERT INTO qualifications(sitting, schoolname,
        examboard, examdate, examno, subjects, grades) VALUES(?, ?, ?, ?, ?, ?,?)");
        $stmt->bind_param("sssssss", $sitting, $schoolname, $examboard, $examdate,
        $examno,  $subjects, $grades);
        $stmt->execute();
        $result = $stmt->affected_rows;
        $stmt -> close();
        $conn -> close();
        if($result)
        {
        header("location:success.php"); // user will be taken to the success page
        }
        else{
            echo "Oops. Something went wrong. Please try again";
        }
    }
?>

0 个答案:

没有答案