I have a problem using a form to submit information into my MySQL database. I’m using a simple test database and form. I know all the DB info and connection info is correct. When you fill out the data and click the Enter Data button, all the info in the textboxes is cleared, but no data is entered into the DB. The code is code I have copied off a website and edited it for my specific needs. Maybe some PHP/MySQL settings? It seems like the following if statement is not being met. Any Suggestions?
// Conditional test to see if submit button was pressed
if ($enter_data)
Here is my code:
<html>
<head><title>Test PHP, MySQL, and Form</title><head>
<body>
<form name=”user_info” method=”post” action=”<? $PHP_SELF ?>”>
Name: <input type=”text” name=”name”>
Sex: <input type=”text” name=”sex”>
Color: <input type=”text” name=”color”>
<input type=”submit” name=”enter_data” value=”Enter Data”>
</form>
</body>
</html>
Processing the form
The following code processes the form when the user clicks the submit button.
Listing 2: Form processing code
<?
// set a variable with the database name
$database_name = "animal";
// attempt to connect to database and store result
$dbh = mysql_connect("localhost","root","dbpassword");
// check the result of the connection attempt
if (!mysql_select_db($database_name)) {
echo "Can't Select $database_name";
}
// Conditional test to see if submit button was pressed
if ($enter_data) {
// build our query string
$sql = “insert into dog (name, sex, color)
values (‘$name’, ‘$sex’, ‘$color’)”;
// execute a query and store the result
$res = mysql_query($sql,$dbh);
// check to make sure the query actually ran
if (!$res) {
echo mysql_errno().": ".mysql_error ()."";
return 0;
}
printf(”<p>Record successfully added</p>”);
}
?>