PHP
  Home arrow PHP arrow Page 7 - PHP 101 (part 4) - Look, Ma...It's Ali...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP 101 (part 4) - Look, Ma...It's Alive!
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2000-09-01

    Table of Contents:
  • PHP 101 (part 4) - Look, Ma...It's Alive!
  • Dumped!
  • Hello Database!
  • Different Strokes...
  • ...For Different Folks
  • What's In A Name?
  • New Friends
  • Today's Special
  • Nuking The People
  • Oops!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    PHP 101 (part 4) - Look, Ma...It's Alive! - New Friends


    (Page 7 of 10 )

    Thus far, we've simply been using SELECT queries to pull information out of a database. But how about putting something in?

    SQL aficionados know that this happens via an INSERT query - and our next example demonstrates how you can use an HTML form to insert data into the database.

    <? // if form has not been submitted, display form if (!$submit) { ?> <html> <head> <basefont face=Arial> </head> <body> <h3>Enter your bookmarks:</h3> <form method="POST" action="push.php4"> <table> <tr> <td> Username </td> <td> <input name="username" length="10" maxlength="30"> </td> </tr> <tr> <td> Web site </td> <td> <input name="title1" length="30" maxlength="30"> </td> </tr> <tr> <td> URL </td> <td> <input name="url1" length="30"> </td> </tr> <tr> <td> Web site </td> <td> <input name="title2" length="30" maxlength="30"> </td> </tr> <tr> <td> URL </td> <td> <input name="url2" length="30"> </td> </tr> <tr> <td> Web site </td> <td> <input name="title3" length="30" maxlength="30"> </td> </tr> <tr> <td> URL </td> <td> <input name="url3" length="30"> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="submit" value="Submit"> </td> </tr> </table> </form> </body> </html> <? } // or process form input else { ?> <?php // connect to database $connection = mysql_connect("localhost", "test", "test") or die("Invalid server or user"); // select database mysql_select_db("php101",$connection); // formulate and run query $query = "insert into url_list(uid,title1,url1,title2,url2,title3,url3) values('$username','$title1','$url1','$title2','$url2','$tit le3','$url3')"; $result = mysql_query($query,$connection) or die("Error in query"); ?> <html> <head> <basefont face=Arial> </head> <body> <center> <h3>Success!</43> <table> <tr> <td> <?php echo $username; ?>'s bookmarks have been saved. </td> </tr> </center> </body> </html> <? } ?>
    This time around, we've demonstrated yet another PHP function - mysql_select_db(), which allows you to specify the database which will be used when connecting to the database. And since you've specified the database, you can use mysql_query() instead of mysql_db_query() - while the latter needs the database name as parameter [as you've seen in the examples above], the former simply needs the query string and the connection identifier.

    As you can see, inserting a record into the database is very straightforward - simply fire the query and your INSERT statement will be executed. The only way to find out if the INSERT was successful is to check the value of the variable $result - if the variable isn't set, it implies that something didn't go as planned.

    More PHP Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway