我似乎在我的代码中遗漏了一些限制post方法和数据未发送到我的数据库的东西。数据库连接测试成功,但是当我提交带有输入数据的表单时...被重定向到new_employee.php,这意味着$ _POST ['提交']为false但表单名称='提交& #39;已经确定了。
这里是php代码
<?php
if (isset($_POST['submit'])) {
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$dept_id = $_POST["dept_id"];
$position = $_POST["position"];
$hire_date = $_POST["hire_date"];
$salary = $_POST["salary"];
$bonus = $_POST["bonus"];
$end_date = $_POST["end_date"];
$sql = "insert into staff (first_name, last_name, dept_id, position, hire_date, salary, bonus, end_date)";
$sql .= " values ('{$first_name}', '{$last_name}', {$dept_id}, '{$position}', {$hire_date}, {$salary}, {$bonus}, {$end_date})";
$result = mysqli_query($connection, $sql);
if ($result) {
redirect_to("employee.php");
} else {
redirect_to("new_employee.php");
}
}?>
这是html表单
<form action="new_employee.php" method="POST" role="form" class="form-horizontal">
<legend>Add new employee</legend>
<div class="col-md-10">
First Name: <input type="text" name="first_name" class="form-control" placeholder="First name">
Last Name: <input type="text" name="last_name" class="form-control" placeholder="Last name">
Department: <input type="text" name="dept_id" class="form-control" placeholder="Department">
Position: <input type="text" name="position" class="form-control" placeholder="Position">
Hire Date: <input type="text" name="hire_date" class="form-control" placeholder="Hire date">
Salary: <input type="text" name="salary" class="form-control" placeholder="Salary">
Bonus: <input type="text" name="bonus" class="form-control" placeholder="Bonus">
End Date: <input type="text" name="end_date" class="form-control" placeholder="Password">
<br/>
<div class="row text-right">
<input type="submit" name="submit" value="Save" class="btn btn-default" />
 
<button type="button" class="btn btn-default" aria-label="Left Align"><a href="employee.php">Cancel</a></button>
</div>
</div>
</form>
我非常感谢能得到的任何帮助。
答案 0 :(得分:0)
我认为您的代码不对。 试试这个sql变量而不是你的:
$sql = "insert into staff (first_name, last_name, dept_id, position, hire_date, salary, bonus, end_date)";
$sql .= " values ('$first_name', '$last_name', $dept_id, '$position', $hire_date, $salary, $bonus, $end_date)";