php - 为什么我的表单输入值没有传递到数据库
问题描述
目前我正在制作一份工作人员休假的申请表。该表格是用户只需要填写他们的 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:  
</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:  
</label>
<input type="number" class="form-control" id="duration" name="leaveDuration" required />  day(s)
</div>
<br>
<div class="form-inline">
<label for="dateFrom">
Date from:  
</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" 中显示错误...
解决方案
当您重定向到另一个页面时,您不能使用 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']);
推荐阅读
- angular - 在 click 方法上从另一个组件调用 app.component.ts 上的方法 - 角度 6
- ios - SwiftUI List 在导航展示后有一个奇怪的过渡
- c# - 使用自定义 WebApplicationFactory 设置集成测试项目
- python - 如何在 zsh 中显示 conda 的环境(不使用 oh-my-zsh)?
- flutter - Flutter 如何在容器中显示 Widget 列表
- c++ - C++ 未知类型名称,即使类型是通过标头定义的
- sql - 使用oracle SQL获取最近两年每个月的最后一天
- javascript - Ajax 请求返回成功,但没有运行服务器端方法
- angular - Angular 5避免针对特定路线的HashLocationStrategy
- python - 从 Python 中的 JSON 响应中获取特定字段