About us Privacy Disclaimer Contact us
FAQ Help Advertising Feedback
Home Sitemap Search Donate us

  Home > Computer & I'net > INSERT...

   Browse by title articles:
   What is hot:

Include

INSERT

SELECT

WHERE

Bad Web Design: Looong Pages
Links
Upload & CHMOD
TimeOut
Prev articles1 2 



INSERT


Computer & I'net articlesINSERT

by David Stanley    



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
<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.

mysql_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.




-----------------
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

  ·Linux

MySQL

  ·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

Rent vs Buy




  Disclaimer | Privacy | Terms of useCopyright © 2004 Nice2know.com