MySQL
  Home arrow MySQL arrow Page 4 - 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 / 276
    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 - Deleting a poll from the database


    (Page 4 of 5 )

    The GetChoice function also allows us to delete a poll by hyper linking us to managepoll.php?method=ShowDelete, which in turns calls the GetPollToDelete function.

    The GetPollToDelete function display a HTML form with a drop-down list containing each poll in the pollQuestions database. The code for the drop-down list looks like this:

    <select name="pollId">

    <option value="0">-- Select Poll --</option>

    <?php

    while($qRow = mysql_fetch_row($qResult))

    {

    ?>

    <option value="<?php echo $qRow[0]; ?>"><?php echo $qRow[1]; ?></option>

    <?php

    }

    ?>

    </select>


    Each option in the drop-down list displays the polls questions. Its value, however, contains the pk_Id value of the poll. I have chosen to use the mysql_fetch_row function to retrieve each poll from the database, however you could also use the mysql_fetch_array function to reference the fields by name instead of number.

    As you can probably guess, the HTML form created by the GetPollToDelete function also tacks a "method" variable onto the end of the <form> tags action attribute, like this:

    <form name="frmDelPoll" action="managepoll.php?method=DeleteFinal" method="post">

    The HTML form generated by the GetPollToDelete function looks like this:

    The HTML form generated by the GetPollToDelete function

    Once we click on the "Delete Selected Poll" button, our PHP script picks up the value of the "method" variable as "DeleteFinal" and calls the DeletePoll method, which actually removes the poll and any votes that visitors may have posted to the pollAnswers table (We will look at voting in the next article).

    The drop-down list that we used to select the poll to delete is called "pollId", and PHP automatically gives us access to its value by creating a variable named $pollId. We need to use a global reference to get access to it:

    global $pollId;

    The drop down list's first option is "-- Select Poll --" and contains the value zero. If $pollId is zero, then the user didn't select a poll to delete. We use a simple if...else statement to check this:

    If($pollId > 0)

    {

    // Poll selected

    }

    else

    {

    // No poll selected

    }


    If the user has selected a poll, we execute two delete queries against our MySQL database: One to remove the poll from the pollQuestions table, and the other to remove any votes from the pollAnswers table that the poll may have attained while it existed in the database:

    $strQuery1 = "DELETE FROM pollQuestions WHERE pk_Id = $pollId";

    $strQuery2 = "DELETE FROM pollAnswers WHERE pollId = $pollId";



    if(!mysql_query($strQuery1))

    die("Couldn't delete from pollQuestions table");



    if(!mysql_query($strQuery2))

    die("Couldn't delete from pollAnswers table");



    ?>

    <h1>Poll Deleted Successfully</h1>

    You have successfully delete poll number <?php echo $pollId;?> from the polls database.

    <br><br>

    <a href="managepoll.php">Continue</a>


    Deleting a poll

    More MySQL Articles
    More By Mitchell Harper


       · This is a nice tutorial. Exactly what I am looking to do.Only problem is, it...
       · nonsense article, without any support material. thanks for trying but you could at...
       · I'm really appreciate that you made this tutorial. It's really helpful except you...
     

    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-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek