MySQL
  Home arrow MySQL arrow Page 2 - Creating a Voting Poll With PHP And MySQL ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
MYSQL

Creating a Voting Poll With PHP And MySQL Part 1/2
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 249
    2002-01-15

    Table of Contents:
  • Creating a Voting Poll With PHP And MySQL Part 1/2
  • Creating the database
  • Adding a poll to the database
  • Deleting a poll from the database
  • Conclusion

  • 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


    Creating a Voting Poll With PHP And MySQL Part 1/2 - Creating the database


    (Page 2 of 5 )

    All of the polls questions and answers will be stored in a MySQL database. Our database will contain two tables: one to store the voting questions, and the other to store our visitor's responses to those questions.

    Start the MySQL console application (located in c:\mysql\bin by default for Windows users, and /usr/local for Linux/Unix users) and enter the following commands to both create and connect to our voting polls new database:

    create database polls;

    use polls;


    Next, we want to create the table to store the voting polls questions. Use the following MySQL commands to do so:

    create table pollQuestions

    (

    pk_Id int auto_increment not null,

    question varchar(100) not null,

    answer1 varchar(50) not null,

    answer2 varchar(50) not null,

    answer3 varchar(50) not null,

    answer4 varchar(50) not null,

    answer5 varchar(50) not null,

    primary key(pk_Id),

    unique id(pk_Id)

    );


    We have just created a new table named "pollQuestions". The details of each of the table’s fields are shown below:
    • pkId: An auto incrementing number, which will allow us to store a unique reference to each specific voting poll question.
    • question: The question that all votes for this poll will be gathered from. The question can be anything you like, just as long as it's 100 characters or less in length, for example: "What do you like most about devArticles.com?" or "Which book will you purchase this week".
    • answer1...5: These fields represent the answers that the users will be able to choose for this poll question. Each of our voting questions can have a maximum of five answers.
    In our "create table..." statement, we also declare the pkId field as a primary key and as a unique identity. Let's now create the table to store our visitor's answers for the poll questions:

    create table pollAnswers

    (

    pk_Id int auto_increment not null,

    pollId int not null,

    answer varchar(50) not null,

    visitorIP varchar(15) not null,

    primary key(pk_Id),

    unique Id(pk_Id)

    );


    The descriptions of the fields in our pollAnswers table are shown below:
    • pkId: An auto incrementing number, which will allow us to store a unique reference to each users poll answers.
    • pollId: The primary key of the poll that this vote is for (i.e. the pk_Id field in the pollQuestions table).
    • answer: The visitors answer to the question with a primary key of pollId.
    • visitorIP: The IP address of the visitor who placed this vote. Storing the visitors IP address is one way to stop the same visitor voting more than once. More on this later.
    As with the pollQuestions table, we declare the pkId field as a primary key and as a unique identity. Use the "describe pollQuestions;" and "describe pollAnswers;" MySQL commands to make sure that both tables were created OK:

    Using the describe command in MySQL

    Before I move onto the next section, let's take a look at the relationship between the pollQuestions table and the pollAnswers table:

    How the question and answer tables are related

    As you can see from the diagram above, the pollId field in the pollAnswers table holds a value that matches the pk_Id field of the poll that the vote is for. Also, the value of the answer field in the pollAnswers table can be either of the values stored in the answer1...5 fields for the poll in the pollQuestions table.

    More MySQL Articles
    More By Mitchell Harper


       · This is a nice tutorial. Exactly what I am looking to do.Only problem is, it...
     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...







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