To insert an item into the database, you can do a simple form and have it submit it to itself. When the submit call is made, it reloads the page. As the page reloads, the PHP IF statement looks for $_POST[‘btnSubmit’] and if it has been pressed, then it will execute the code to insert the data into the database.
If you are unsure how to open up a connection to the database, please go here:
PHP Simple MySQL Open Configure and Close the Connection to a Database
-
<?php
-
if(isset($_POST['btnSubmit']))
-
{
-
include 'config.php';
-
include 'opendb.php';
-
-
$first = $_POST['txtFirst'];
-
$last = $_POST['txtLast'];
-
-
$myquery = "INSERT INTO tblTest (tstFirstName, tstLastName)
-
VALUES ('$_POST[txtFirst]', '$_POST[txtLast]')";
-
-
mysql_query($myquery) or
die('Error, insert query failed');
//order executes
-
-
include 'closedb.php';
-
-
}
-
?>
-
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
-
</head>
-
-
-
<form action="" method="post" name="form1">
-
-
-
-
First Name:
-
</td>
-
-
<input type="text" name="txtFirst" />
-
</td>
-
</tr>
-
-
-
Last Name:
-
</td>
-
-
<input type="text" name="txtLast" />
-
</td>
-
</tr>
-
-
-
</td>
-
</tr>
-
-
-
<input type="submit" name="btnSubmit" id="btnSubmit" value="Insert Name" />
-
</td>
-
</tr>
-
</table>
-
</form>
-
</body>