I've written this code to add data from the form to the database, but nothing happens. I don't know what is wrong, please help me check. Been trying to find the solution for the past two days, and I need to submit this asap!
<?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>
This is my updated code. Now I received an error saying "Table 'syllabus.subject' doesn't exist" after I click the button.
ifstatement if you're seeing thealert(). However, you still have more debugging to do. Is there an error? What is the return value ofmysql_query()? What is the actual query being executed after the values are added to it? (After all, you may be accidentally taking advantage of your SQL injection vulnerability, resulting in undefined behavior.)mysql_query("insert into subject (Sub_Code,Sub_Name,Sub_Credit_Hr,Course) values ('$subcode','$subname',$credithour,'$course')") or die(mysq_error());and let me know if it shows any error.