当我提交SQL查询时,数据库中没有创建记录,但也没有显示错误消息。
**很抱歉长代码,因为我不知道代码的问题在哪里,我也掌握了编程的基本知识。
我使用2个不同的表进行查询和插入:
<?php
if(isset($send))
{
$today=date('Y-m-d');
$fname=isset ($_POST['fname']) ? $_POST['fname'] : '';
$fname=mysqli_real_escape_string($con, $fname);
$sql=mysqli_query("SELECT * FROM kc54i_workforce_employees WHERE fname='$fname'");
$row=mysqli_fetch_array($sql);
$namestaff=$row['lname'];
$sql2 = "
INSERT INTO `avenue`.`staff_movement` (`id`, `staff_no`, `department`, `name`, `status`, `category`, `dateStart`, `dateEng`)
VALUES (NULL, '$fname','$department','$namestaff','$status','$category','$dateStart','$dateEnd');";
if(@mysqli_query($con, $sql2))
{
echo "<script type='text/javascript'>\n";
echo "alert('New record created successfully');\n";
echo "window.navigate('add_move_test.php');";
echo "</script>";
}
else
echo "Error: " . $sql2 . "<br>" . mysqli_error();
}
else
?>
<html>
<body>
<form name="add_move_test.php" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return formCheck(this);">
<table>
<tr>
<tr>
<td valign="top">Department</td>
<td valign="top">
<select name="department" onchange='this.form.submit();'>
<?php $department=isset($_POST[ 'department']) ? $_POST[ 'department'] : '';
$department=mysqli_real_escape_string($con, $department); ?>
<option value="">-Select One-</option>
<option value="7" <?php if ($department=="7" ) echo "selected";?>>Management</option>
<option value="2" <?php if ($department=="2" ) echo "selected";?>>Administration</option>
<option value="5" <?php if ($department=="5" ) echo "selected";?>>Human Resource</option>
<option value="25" <?php if ($department=="25" ) echo "selected";?>>Business Development</option>
<option value="12" <?php if ($department=="12" ) echo "selected";?>>Finance</option>
<option value="19" <?php if ($department=="19" ) echo "selected";?>>Procurement</option>
<option value="1" <?php if ($department=="1" ) echo "selected";?>>IT Support</option>
<option value="6" <?php if ($department=="6" ) echo "selected";?>>Project</option>
<option value="13" <?php if ($department=="13" ) echo "selected";?>>Project(Control)</option>
<option value="10" <?php if ($department=="10" ) echo "selected";?>>Project(Management)</option>
<option value="8" <?php if ($department=="8" ) echo "selected";?>>Project(Transportation)</option>
<option value="24" <?php if ($department=="24" ) echo "selected";?>>Construction</option>
<option value="16" <?php if ($department=="16" ) echo "selected";?>>Construction(Management)</option>
<option value="4" <?php if ($department=="4" ) echo "selected";?>>Construction(Planning & Cost Estimation)</option>
<option value="18" <?php if ($department=="18" ) echo "selected";?>>Engineering</option>
<option value="15" <?php if ($department=="15" ) echo "selected";?>>Engineering(Civil)</option>
<option value="14" <?php if ($department=="14" ) echo "selected";?>>Engineering(Fire & Gas)</option>
<option value="23" <?php if ($department=="23" ) echo "selected";?>>Engineering(Electrical)</option>
<option value="22" <?php if ($department=="22" ) echo "selected";?>>Engineering(Instrument)</option>
<option value="3" <?php if ($department=="3" ) echo "selected";?>>Engineering(Mechanical)</option>
<option value="9" <?php if ($department=="9" ) echo "selected";?>>Engineering(Layout & Piping)</option>
<option value="20" <?php if ($department=="20" ) echo "selected";?>>Engineering(Process)</option>
<option value="17" <?php if ($department=="17" ) echo "selected";?>>HSSE</option>
<option value="11" <?php if ($department=="11" ) echo "selected";?>>Quality(QA/QC)</option>
</select>
</td>
</tr>
<tr>
<td valign="top">Name</td>
<td valign="top">
<select name="fname">
<option selected>-Select Name-</option>
<?php $sql="select * from kc54i_workforce_employees where department='$department' ORDER BY lname ASC" ;
$results=mysqli_query($con, $sql);
while ($row=mysqli_fetch_array($results)) { $department=$row[ 'department'];
$name=$row[ 'lname']; $fname=$row['fname']; ?>
<option value="<?php echo $fname;?>">
<?php echo $name;?>
</option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td valign="top">Category</td>
<td valign="top">
<select name="category">
<?php $category=i sset($_POST[ 'category']) ? $_POST[ 'category'] : ''; $category=mysqli_real_escape_string($con, $category); ?>
<option value="">-Select One-</option>
<option value="office" <?php if ($category=="office" ) echo "selected";?>>Office</option>
<option value="site" <?php if ($category=="site" ) echo "selected";?>>Site</option>
</select>
</td>
</tr>
<tr>
<td valign="top">Date Start</td>
<td valign="top">
<script>
DateInput('dateStart', true)
</script>
</td>
</tr>
<tr>
<td valign="top">Date End</td>
<td valign="top">
<script>
DateInput('dateEnd', true)
</script>
</td>
</tr>
<tr>
<td valign="top">Status</td>
<td valign="top">
<input name="status" type="text" id="status" size="50" maxlength="50">
</td>
</tr>
<tr align="center" valign="middle">
<td colspan="2">
<input name="send" type="submit" id="send" value="Send">
<input name="clear" type="reset" id="clear" value="Clear">
<input type="button" name="back" value="Back" onClick="javascript:history.back();">
</td>
</tr>
</table>
</form>
</body>
</html>
答案 0 :(得分:0)
isset
函数之间有空间所有东西都已存在,我只是运行它,因为我没有你的表结构,
<html>
<body>
<form name="add_move_test.php" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return formCheck(this);">
<table>
<tr>
<tr>
<td valign="top">Department</td>
<td valign="top">
<select name="department" onchange='this.form.submit();'>
<?php
$department=isset($_POST[ 'department']) ? $_POST[ 'department'] : '';
$department=mysqli_real_escape_string($con, $department); ?>
<option value="">-Select One-</option>
<option value="7" <?php if ($department=="7" ) echo "selected";?>>Management</option>
<option value="2" <?php if ($department=="2" ) echo "selected";?>>Administration</option>
<option value="5" <?php if ($department=="5" ) echo "selected";?>>Human Resource</option>
<option value="25" <?php if ($department=="25" ) echo "selected";?>>Business Development</option>
<option value="12" <?php if ($department=="12" ) echo "selected";?>>Finance</option>
<option value="19" <?php if ($department=="19" ) echo "selected";?>>Procurement</option>
<option value="1" <?php if ($department=="1" ) echo "selected";?>>IT Support</option>
<option value="6" <?php if ($department=="6" ) echo "selected";?>>Project</option>
<option value="13" <?php if ($department=="13" ) echo "selected";?>>Project(Control)</option>
<option value="10" <?php if ($department=="10" ) echo "selected";?>>Project(Management)</option>
<option value="8" <?php if ($department=="8" ) echo "selected";?>>Project(Transportation)</option>
<option value="24" <?php if ($department=="24" ) echo "selected";?>>Construction</option>
<option value="16" <?php if ($department=="16" ) echo "selected";?>>Construction(Management)</option>
<option value="4" <?php if ($department=="4" ) echo "selected";?>>Construction(Planning & Cost Estimation)</option>
<option value="18" <?php if ($department=="18" ) echo "selected";?>>Engineering</option>
<option value="15" <?php if ($department=="15" ) echo "selected";?>>Engineering(Civil)</option>
<option value="14" <?php if ($department=="14" ) echo "selected";?>>Engineering(Fire & Gas)</option>
<option value="23" <?php if ($department=="23" ) echo "selected";?>>Engineering(Electrical)</option>
<option value="22" <?php if ($department=="22" ) echo "selected";?>>Engineering(Instrument)</option>
<option value="3" <?php if ($department=="3" ) echo "selected";?>>Engineering(Mechanical)</option>
<option value="9" <?php if ($department=="9" ) echo "selected";?>>Engineering(Layout & Piping)</option>
<option value="20" <?php if ($department=="20" ) echo "selected";?>>Engineering(Process)</option>
<option value="17" <?php if ($department=="17" ) echo "selected";?>>HSSE</option>
<option value="11" <?php if ($department=="11" ) echo "selected";?>>Quality(QA/QC)</option>
</select>
</td>
</tr>
<tr>
<td valign="top">Name</td>
<td valign="top">
<select name="fname">
<option selected>-Select Name-</option>
<?php $sql="select * from kc54i_workforce_employees where department='$department' ORDER BY lname ASC" ;
$results=mysqli_query($con, $sql);
while ($row=mysqli_fetch_array($results)) {
$department=$row[ 'department'];
$name=$row[ 'lname']; $fname=$row['fname']; ?>
<option value="<?php echo $fname;?>">
<?php echo $name;?>
</option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td valign="top">Category</td>
<td valign="top">
<select name="category">
<?php $category=isset($_POST[ 'category']) ? $_POST[ 'category'] : ''; $category=mysqli_real_escape_string($con, $category); ?>
<option value="">-Select One-</option>
<option value="office" <?php if ($category=="office" ) echo "selected";?>>Office</option>
<option value="site" <?php if ($category=="site" ) echo "selected";?>>Site</option>
</select>
</td>
</tr>
<tr>
<td valign="top">Date Start</td>
<td valign="top">
<script>
DateInput('dateStart', true)
</script>
</td>
</tr>
<tr>
<td valign="top">Date End</td>
<td valign="top">
<script>
DateInput('dateEnd', true)
</script>
</td>
</tr>
<tr>
<td valign="top">Status</td>
<td valign="top">
<input name="status" type="text" id="status" size="50" maxlength="50">
</td>
</tr>
<tr align="center" valign="middle">
<td colspan="2">
<input name="send" type="submit" id="send" value="Send">
<input name="clear" type="reset" id="clear" value="Clear">
<input type="button" name="back" value="Back" onClick="javascript:history.back();">
</td>
</tr>
</table>
</form>
</body>
</html>