neza
neza

Reputation: 1

failed to add data to database from form in PHP

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.

Upvotes: 0

Views: 405

Answers (2)

Ramon Saraiva
Ramon Saraiva

Reputation: 518

You should write the method and the action on your form tag.

<form method="post" action="">

Upvotes: 1

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

You form is being submitted in GET because you have not specified a method="" to your form.

Change

<form>

To

<form method="post">

It might not be all, but at least you will be able to continue with this...

ADD THIS

if(mysql_affected_rows() == 0){
    echo mysql_error();
}

Upvotes: 1

Related Questions