The INSERT command is used to enter new information into a database. A common way to gather information is using a form.
form_page.php
Once the SUBMIT button is clicked on, the information entered will pass over to the "enter_it.php" page through the $_POST[ ] variable.
enter_it.php
To explain the new coding snippit :
We are using a variable $query to contain the command information we want to perform. This keeps our coding easy to maintain.
INSERT INTO prepares the database for new incoming data. address_book is stating which table is going to receive the new information and what columns (by name) will be affected.
The VALUES of the following variables contain the new information to be entered into the specified columns.
IF the following value is not true, the script will stop (die) and print out the error.
mysql_db_query is a command used to perform the actual insert command. it uses the database name, query command, and server link to make it all happen.
form_page.php
<form method="post" action="enter_it.php">
First Name : <input type="text" name="first_name" /><br />
Last Name : <input type="text" name="last_name" /><br />
Phone Number : <input type="text" name="phone_number" /><br />
<input type="submit" name="submit" value="submit" />
</form>
First Name : <input type="text" name="first_name" /><br />
Last Name : <input type="text" name="last_name" /><br />
Phone Number : <input type="text" name="phone_number" /><br />
<input type="submit" name="submit" value="submit" />
</form>
Once the SUBMIT button is clicked on, the information entered will pass over to the "enter_it.php" page through the $_POST[ ] variable.
enter_it.php
<?php
// connect to server, database, table.
include ("db_connect.inc");
// add the new information into the database
$query="INSERT INTO address_book (first_name,last_name,phone_number) VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[phone_number]')";
if(!mysql_db_query($dbname,$query,$link_id)) die(mysql_error());
echo "success in database entry.";
echo "<br />";
echo "<a href=\"form_page.php\">Click here to return to the form page.</a>";
?>
// connect to server, database, table.
include ("db_connect.inc");
// add the new information into the database
$query="INSERT INTO address_book (first_name,last_name,phone_number) VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[phone_number]')";
if(!mysql_db_query($dbname,$query,$link_id)) die(mysql_error());
echo "success in database entry.";
echo "<br />";
echo "<a href=\"form_page.php\">Click here to return to the form page.</a>";
?>
To explain the new coding snippit :
$query="INSERT INTO address_book (first_name,last_name,phone_number) VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[phone_number]')";
We are using a variable $query to contain the command information we want to perform. This keeps our coding easy to maintain.
$query="INSERT INTO address_book (first_name,last_name,phone_number) VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[phone_number]')";
INSERT INTO prepares the database for new incoming data. address_book is stating which table is going to receive the new information and what columns (by name) will be affected.
$query="INSERT INTO address_book (first_name,last_name,phone_number) VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[phone_number]')";
The VALUES of the following variables contain the new information to be entered into the specified columns.
if(!mysql_db_query($dbname,$query,$link_id)) die(mysql_error());
IF the following value is not true, the script will stop (die) and print out the error.
mysql_db_query is a command used to perform the actual insert command. it uses the database name, query command, and server link to make it all happen.


