| Home > Computer & I'net > INSERT... |
|
|
|||||||||||||
|
|
|||||||||||||
|
||||||||||||||
INSERT | |
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> 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_query($dbname,$query,$link_id)) die(sql_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_query($dbname,$query,$link_id)) die(sql_error());
IF the following value is not true, the script will stop (die) and print out the error. ----------------- Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission. | |
| Articles |
•Auto & Trucks•Business•Computer & I'net·General·Apache·CSS·Database·Hardware·HTML·Javascript&DHTML·LinuxMySQL·Operating System·Perl / CGI·PHP·Programming·Publishing·Search Engines·Software Problems·SSI·Tips & Tricks·Utilities·Web Design•Family•Food & Drink•Gardening•Health•Other•Pets•Psychology•Spiritual•Travel•Women |
| Calculators |
|