PHP
  Home arrow PHP arrow Page 9 - Democracy, The PHP Way
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Democracy, The PHP Way
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2001-04-16

    Table of Contents:
  • Democracy, The PHP Way
  • The Plan
  • Design View
  • Start Me Up
  • Vote Now, Or Forever Hold Your Peace
  • The Number Game
  • Down Memory Lane
  • Cookie-Cutter Code
  • Adding More...

  • 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


    Democracy, The PHP Way - Adding More...


    (Page 9 of 9 )

    The final item on today's menu is perhaps the simplest - a form which allows administrators to easily add new questions to the system.

    This script, named "add.php", is divided into two sections. The initial section is the form itself, with fields for the question and possible responses.

    <html> <head> <basefont face="Arial"> </head> <body bgcolor="white"> <? // this script is meant only for the administrator - add new polls to the database // $submit does not exist -> forms has not been submitted -> display initial page if (!$submit) { ?> <table border="0" cellspacing="5" cellpadding="5"> <form action="add.php" method="post"> <tr> <td>Poll question<br><input type="Text" name="question" size="25"></td> </tr> <tr> <td>Response 1<br><input type="Text" name="response1" size="25"></td> </tr> <tr> <td>Response 2<br><input type="Text" name="response2" size="25"></td> </tr> <tr> <td>Response 3<br><input type="Text" name="response3" size="25"></td> </tr> <tr> <td align=center><input type=submit name=submit value="Add Question"></td> </tr> </form> </table> <? } // form has been submitted - process data else { // form processing code goes here } ?> </body> </html>

    Once the form has been submitted, the data from the form fields is assimilated into an SQL query and INSERTed into the database.

    <html> <head> <basefont face="Arial"> </head> <body bgcolor="white"> <? // this script is meant only for the administrator - add new polls to the database // $submit does not exist -> forms has not been submitted -> display initial page if (!$submit) { // initial form goes here } // form has been submitted - process data else { // includes include("config.php"); include("common.php"); // connect and insert form data into database $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); $query = "INSERT INTO $table (question, response1, response2, response3, date) VALUES ('$question', '$response1', '$response2', '$response3', NOW())"; $result = mysql_db_query($database, $query, $connection) or die ("Could not execute query: $query. " . mysql_error()); // check for result code if ($result) { echo "<i>Entry successfully added. Click here to <a href=start.php>view</a></i>"; } else { echo "<i>Error! Please <a href=add.php>try again</a></i>"; } // close connection mysql_close($connection); } ?> </body> </html>

    And here's what it looks like:


    And that's about it. Hopefully, this exercise gave you some insight into how PHP can be used to build a simple Web application, and illustrated its power and flexibility as a rapid development tool for the Web medium. You can use the example scripts above to build your own simple poll, or even modify them a little bit and create an online quiz (it *is* the same basic principle - one question, three answers). Either way, have fun...and stay healthy!

    This article copyright Melonfire 2001. All rights reserved.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT