Hello, I am new to php and mysql. I am having difficulties with using a php form on my website to submit data to a mysql database.
This is the code I have for the form on the webpage:
Code: Select all
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" value="Submit" />
</form>
This is all the code from insert.php that the form calls for:
Code: Select all
<?php
$con = mysql_connect("localhost","christia_form","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("christia_sermons", $con);
$sql="INSERT INTO Persons (Firstname, Lastname, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
When I enter and submit the form data on my webpage, the form refreshes itself, but nothing else happens. However, if I use the 'get' method instead of the 'post' method, and insert the php code directly in the form tag on the webpage (ie. <form action="PHP CODE" method="get">), then it will submit the data to the database, but it creates other problems, like every time the page is refreshed, it will automatically submit an entry to the database. To me, it seems like the form is not calling up insert.php, but I have tried everything that I know of to no avail. Please help!