首页 > 解决方案 > 为什么我的表单输入值没有传递到数据库

问题描述

目前我正在制作一份工作人员休假的申请表。该表格是用户只需要填写他们的 leaveDuration、leaveDate 和原因。我需要将他们在输入中填写的值传递给数据库,以便我可以检索。用户完成提交后,会有一条消息说已提交并重定向到 home.php。这个问题已经困扰了我好几天了...感谢您的帮助,感谢您的帮助...

这是我目前在将值传递到名为 leave_management 的数据库时遇到问题的代码 我放置在 form.php 顶部的函数

<?php 
    $db = mysqli_connect("localhost", "root", "", "leave_management");
    if($db === false)
    die("ERROR: Could not connect. ");
    if(isset($_POST['submit_btn']))
    {
        $ID             =  $_SESSION['user']['ID'];
        $leaveType      =  $_POST['leaveType'];
        $leaveDuration  =  $_POST['leaveDuration'];
        $dateFrom       =  $_POST['dateFrom'];
        $reason         =  $_POST['reason'];
        $status;
        $query = "INSERT INTO form (ID, formID, leaveType, leaveDuration, dateFrom, reason, status, dateApprove, approveBy, remark, Created, Modified) 
        VALUES ('$ID', NULL, '$leaveType', '$leaveDuration', '$dateFrom', '$reason', 'Pending', '', NULL, '', '', '')";
        //mysqli_query($db, $query);
        if(mysqli_query($db ,$query)){
            echo "Records inserted successfully.";
        } else{
            echo "ERROR";
        }
        header("Location: home.php");
    }   
?>

这是用户需要填写的表格

            <form role="form" method="post" action="form.php">
                <div class="form-group">
                    <label for="employeeName">
                        Employee Name:  
                    </label>
                    <input type="text" class="form-control" value="<?php echo $_SESSION['user']['name']?>" id="employeeName" name="name" disabled />
                </div>
                <div class="form-group">
                    <label for="ICno">
                        IC Number:  
                    </label>
                    <input type="text" class="form-control" value="<?php echo $_SESSION['user']['IC']?>" id="ICno" name="IC" disabled />
                </div>
                <div class="form-group">
                    <label for="selectLeave">
                        Apply Leave for: &ensp; 
                    </label>
                    <select name="leaveType" required>
                        <option value="Annual Leave">Annual Leave</option>
                        <option value="Unpaid Leave">Unpaid Leave</option>
                        <option value="Sick Leave">Sick Leave</option>
                    </select>
                </div>
                <div class="form-inline">
                    <label for="duration">
                        Duration:  &ensp;
                    </label>
                    <input type="number" class="form-control" id="duration" name="leaveDuration" required /> &ensp;day(s)
                </div>
                <br>
                <div class="form-inline">
                    <label for="dateFrom">
                        Date from:  &ensp;
                    </label>
                    <input type="date" class="form-control" id="datetimepicker" name="dateFrom" required /> 
                </div>
                <br>
                <div>
                    <label for="reason">
                    Reason:  
                    </label>
                    <textarea name="reason" class="form-control" id="reason" rows="5" cols="40" required></textarea>
                </div>
                <br>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" required /> I had understand and agree with the <a href="protocol.php">Leave Office Protocol and Term and Condition</a>.
                    </label>
                </div> 
                <input type="submit" class="btn btn-primary" name="submit_btn"/>
            </form>

这是表格的数据库。 在此处输入图像描述

它在 echo "ERROR" 中显示错误...

标签: phphtmlmysql

解决方案


当您重定向到另一个页面时,您不能使用 echo 并且它不起作用......相反,您可以设置一个会话并在您的目标页面中显示它,例如:

    $result = mysqli_query($db, $query);
    if($result){
        $_SESSION['success'] = 'Records inserted successfully.';
    } else{
        $_SESSION['error'] = 'Error';
    }
    header("Location: home.php");

然后,在您的 home.php 页面中:

if (isset($_SESSION['success']))
    echo $_SESSION['success'];
elseif (isset($_SESSION['error']))
    echo $_SESSION['error'];

unset($_SESSION['success'],$_SESSION['error']);

推荐阅读