View Single Post
  #2  
Old 05-04-2009, 05:48 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,566
Insert Data From a Form Into a Database

Now we will create an HTML form that can be used to add new records to the "Persons" table.




Here is the HTML form:






Firstname:
Lastname:
Age:






When a user clicks the submit ****on in the HTML form in the example above, the form data is sent to "insert.php".


The "insert.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables.


Then, the mysql_query() function executes the INSERT INTO statement, and a new record will be added to the "Persons" table.


Here is the "insert.php" page:




$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} mysql_select_db("my_db", $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)
?>
Reply With Quote