目前,我正在处理员工请假的申请表。该表格是用户仅需填写其LeaveDuration,离开日期和原因。我需要将它们填写在输入中的值传递给数据库,以便检索。用户完成提交后,将显示一条消息,表示已提交并重定向到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>
它在回显“ ERROR”中显示ERROR ...
答案 0 :(得分:3)
当您重定向到另一个页面时,您将无法使用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']);
答案 1 :(得分:1)
您已在表格中将auto-increment
和not null
设为formID列。而且,您正在通过PHP代码将null
传递给它。
也看看如何获取MySQL错误
https://www.w3schools.com/php/func_mysqli_error.asp
您的代码完全容易受到SQL注入
了解有关PHP Prepared语句的信息。 https://www.w3schools.com/php/php_mysql_prepared_statements.asp
注意:
然后您知道的列有时可以包含或不包含值,您可以在数据库中将其标记为NULL
您的查询应类似于:
$query = "INSERT INTO form (ID, leaveType, leaveDuration, dateFrom, reason, status) VALUES ('$ID','$leaveType', '$leaveDuration', '$dateFrom', '$reason', 'Pending')";