PHP
  Home arrow PHP arrow Page 7 - PHP 101 (part 4) - Look, Ma...It's Alive!
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT