我编写了这段代码来将表单中的数据添加到数据库中,但没有任何反应。不知道哪里出了问题,请帮我检查一下。这两天一直在寻找解决方案,我需要尽快提交!
<?php
include("connectDB.php");
if (isset($_POST["savebtn"]))
{
$subcode= $_POST["sub_code"];
$subname = $_POST["sub_name"];
$credithour= $_POST["sub_credit_hr"];
$course = $_POST["course"];
mysql_query("insert into subject (Sub_Code,Sub_Name,Sub_Credit_Hr,Course) values
('$subcode','$subname',$credithour,'$course')") or die(mysql_error());
if(mysql_affected_rows() == 0)
{
echo mysql_error();
}
?>
<script type = "text/javascript">
alert("Record saved.");
</script>
<?php
}
?>
<html>
<body>
<form method="post">
<table border="1" width="70%">
<tr>
<td width="20%">Subject Code</td>
<td width="3%">:</td><td width="60%"><input type="text" name="sub_code"></td>
</tr>
<tr>
<td>Subject Name</td>
<td>:</td><td><input type="text" name="sub_name"></td>
</tr>
<tr>
<td>Subject Credit Hour</td>
<td>:</td><td><input type="text" name="sub_credit_hr"></td>
</tr>
<tr>
<td>Course</td>
<td>:</td>
<td><select name="course">
<option>Information Technology</option>
<option>Business Administration</option>
<option>Engineering</option>
</select></td>
</tr>
</table>
<br>
<input type="submit" name="savebtn" value="Save Record">
</form>
</body>
</html>
这是我更新的代码。现在,在单击按钮后,我收到一条错误消息“表‘syllabus.subject’不存在”。
最佳答案
您的表单正在 GET 中提交,因为您尚未为表单指定 method=""。
改变
<form>
至
<form method="post">
这可能不是全部,但至少你将能够继续这个......
添加此内容
if(mysql_affected_rows() == 0){
echo mysql_error();
}
关于php - 无法从 PHP 表单将数据添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8728903/