0

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.

7
  • Define "nothing happens." Does the database return an error? Does the code throw an error? If you debug through the code, at what point does it deviate from expected behavior? Keep in mind that you also have a wide open SQL injection vulnerability. You're lucky that "nothing" is all that's happening. Commented Jan 4, 2012 at 15:03
  • As I insert data in the form, and clicked the save button, a javascript alert message pops up saying record added, but when i check in my database, there is no data inserted. Commented Jan 4, 2012 at 15:14
  • It sounds like at least now you're getting into the if statement if you're seeing the alert(). However, you still have more debugging to do. Is there an error? What is the return value of mysql_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.) Commented Jan 4, 2012 at 15:18
  • There is no error. How is mine subject to SQL injection vulnerability? Commented Jan 4, 2012 at 15:30
  • can you replace your mysql query code with this 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. Commented Jan 4, 2012 at 16:07

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Mathieu. My bad. Didn't realize I put the method in the table tag. Well, it does get me started, because the javascript alert message has come out, but still, no data is added.
Enable error_reporting and check to see if you have a MYSQL error popping out?
I have added that, and no, there's still no error. How do I know if my database has successfully been linked? Could that be the problem?
When you do mysql_connect(), do you check if you have an error? such as "if(mysql_connect(...) == false){ echo 'error connecting to server'; }
Then we can't help you more than that... no one can fix a problem where there is no error or nothing to point us towards a minimal solution... And neither will you. You need to find a way to at least generate an error, generate an SQL and try it out in phpmyadmin or something like that
|
1

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

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

6 Comments

I'm sorry but what is php_self? Anyway, i did that and then a new page comes out saying Forbidden - You don't have permission to access /< on this server.
First, $PHP_SELF doesn't exist and second it is useless to put PHP_SELF (thats the real one) because NOT specifying and ACTION results in calling back the same page which is often MUCH better...
$PHP_SELF is not supposed to exist. PHP_SELF is a constant, should not be prefixed with $. And furthermore, the snippet of code doesn't even output... <?php echo PHP_SELF; ?> would work or <?=PHPSELF?>.
-1 for your answer, it is not $PHP_SELF it should be $_SERVER['PHP_SELF'], and specifying the action is not necessary as @Mathieu Dumoulin pointed out leaving it out will result in calling back the same page.
@IbrahimAzharArmar Doesn't PHP_SELF constant exist too, i see that often and it maps to $_SERVER['PHP_SELF'] i think, not sure though...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.