有什么理由说这不起作用吗?
<form method="post" action="CorseProject.php">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">EmployeeID</td>
<td><input name="employeeid" type="text" id="employeeid" required oninvalid="this.setCustomValidity('You must enter an employee id')" onchange="this.setCustomValidity('')"></td>
</tr>
<tr>
<td width="100">ManagementID</td>
<td><input name="managementid" type="text" id="managementid" required oninvalid="this.setCustomValidity('You must enter a management id')" onchange="this.setCustomValidity('')"></td>
</tr>
<tr>
<td width="100">First Name</td>
<td><input name="first_name" type="text" id="first_name" required oninvalid="this.setCustomValidity('You must enter a first name')" onchange="this.setCustomValidity('')"></td>
</tr>
<tr>
<td width="100">Last Name</td>
<td><input name="last_name" type="text" id="last_name" required oninvalid="this.setCustomValidity('You must enter a last name')" onchange="this.setCustomValidity('')"></td>
</tr>
<tr>
<td width="100">Age</td>
<td><input name="age" type="text" id="age" required oninvalid="this.setCustomValidity('You must enter an age)" onchange="this.setCustomValidity('')"></td>
</tr>
<tr>
<td width="100">Employment Period in Months</td>
<td><input name="employment_period" type="text" id="employment_period" required oninvalid="this.setCustomValidity('You must enter an employment period')" onchange="this.setCustomValidity('')"></td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="find" type="submit" id="add" value="Insert New Record"></td>
</tr>
</table>
</form>
下面的我的配置和opendb文件工作正常,因此它们不会导致此错误。
<?php
include 'mod2_config.php';
include 'mod2_opendb.php';
$employeeid = (isset($_POST['employeeid'])? $_POST['employeeid']:'');
$managementid = (isset($_POST['managementid']) ? $_POST['managementid']: '');
$fname= (isset($_POST['first_name']) ? $_POST['first_name'] : '');
$lname= (isset($_POST['last_name']) ? $_POST['last_name'] : '');
$age= (isset($_POST['age']) ? $_POST['age'] : '');
$employment_period= (isset($_POST['employment_period']) ? $_POST['employment_period'] : '');
$sql= " INSERT INTO employees (employeeid, managementid, first_name,last_name, age, employement_period)
VALUES ('$employeeid','$managementid',$fname','$lname','$age', '$employment_period')";
$result = mysqli_query($con, $sql);
$sql= " SELECT employeeid, managementid, first_name, last_name, age,employment_period from employees WHERE employeeid = $employeeid LIMIT 1";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<b>Record successfully inserted:</b><br>";
echo "<b>EmployeeID: " . $row["employeeid"]. "</b><br>";
echo "<b>ManagementID: " . $row["managementid"]. "</b><br>";
echo "<b>Name: " . $row["first_name"]. " " . $row["last_name"]. "</b><br>";
echo "<b>Age: " . $row["age"]. "</b><br>";
echo "<b>Employment Period In Months: " . $row["employment_period"]. "</b><br>";
}
}
else {
echo "Sorry there are no matches! Please check your entry and try again.";
}
//The else statement above keeps running instead of the form inserting information into the database and showing the results.
mysqli_close($con);
?>
我已经尝试了几个小时的插入脚本,但仍然无法将其插入到数据库中。
答案 0 :(得分:1)
试试这个。
<?php
include 'mod2_config.php';
include 'mod2_opendb.php';
$employeeid= (isset($_POST['employeeid']) ? $_POST['employeeid'] : '');
$managementid= (isset($_POST['managementid']) ? $_POST['managementid'] : '');
$fname= (isset($_POST['first_name']) ? $_POST['first_name'] : '');
$lname= (isset($_POST['last_name']) ? $_POST['last_name'] : '');
$age= (isset($_POST['age']) ? $_POST['age'] : '');
$employment_period= (isset($_POST['employment_period']) ? $_POST['employment_period'] : '');
$sql_insert = " INSERT INTO employees (employeeid, managementid, first_name, last_name, age, employement_period)
VALUES ('$employeeid','$managementid','$fname','$lname','$age', '$employment_period')";
$result_insert = mysqli_query($con, $sql_insert);
$sql_fetch = " SELECT * from employees WHERE employeeid = $employeeid LIMIT 1";
$result_fetch = mysqli_query($con, $sql_fetch);
if (mysqli_num_rows($result_fetch) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result_fetch)) {
echo "<b>Record successfully inserted:</b><br>";
echo "<b>EmployeeID: " . $row["employeeid"]. "</b><br>";
echo "<b>ManagementID: " . $row["managementid"]. "</b><br>";
echo "<b>Name: " . $row["first_name"]. " " . $row["last_name"]. "</b><br>";
echo "<b>Age: " . $row["age"]. "</b><br>";
echo "<b>Employment Period In Months: " . $row["employment_period"]. "</b><br>";
}
}
else {
echo "Sorry there are no matches! Please check your entry and try again.";
}
答案 1 :(得分:0)
错误位于 $ fname
的插入查询中将 $ fname'替换为'$ fname'
$sql= " INSERT INTO employees (managementid, first_name,last_name, age, employement_period)
VALUES ('$managementid','$fname','$lname','$age', '$employment_period')";
这样做
然后像这样编写选择查询。
$sql= " SELECT employeeid, managementid, first_name, last_name, age,employment_period from employees ORDER BY employeeid DESC LIMIT 1";
它会为您提供所需的结果....