I'm working on a simple PHP form and I'm running into a bit of a strange problem. It's occurring both on my local MAMP environment and my hosted website. The purpose of this script is to store a record into a table based on data submitted via the following form.
<?php
include("dbconnect.php");
if(isset($_POST['submit']){
$firstName = $_POST['firstName'];
$SQLString = "INSERT INTO name_table (first_name) VALUES($firstName)";
mysql_query($SQLString);
} else {
//Debugging script
$mypostdata = file("php://input");
print "<pre>";
var_dump($_POST);
var_dump($mypostdata);
print "</pre>";
}
?>
<form name="myForm" action="<?php $_SERVER['PHP_SELF']?>" method="POST">
<label for="firstName">First Name:</label>
<input type="text" name="firstName" value="Bob" />
<input type="submit" name="submit" value="Finish">
</form>
using var_dump on $_POST
outputs this:
array(2) {
["firstName"]=>
string(3) "Bob"
["submit"]=>
string(6) "Finish"
}
var_dump on $mypostdata = file("php://input");
results in this:
array(9){
"name=Bob&submit=Finish"
}
When I submit the form no record is being created in the table.
Edit: For clarity I originally thought the error laid with the $_POST['myForm']
name coming up as NULL
but comments and answers have cleared that up.
http://www.w3.org/TR/html401/interact/forms.html#successful-controls
Form itself isn't listed as "Successful control" (see 17.2.1), hences its name shouldn't be submitted.
If first_name is a varchar, then it needs to be wrapped in single quotes in the MySQL query. E.g.:
"INSERT INTO name_table (first_name) VALUES('$firstName')";
change $SQLString = "INSERT INTO name_table (first_name) VALUES($firstName)"; to $SQLString = "INSERT INTO name_table (first_name) VALUES(".$firstName.")";
if it not works display page errors by writing error_reporting(E_ALL); and ini_set("display_errors",1);
Keep posting the results