Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Poll PHP

  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Poll PHP

    Hi,

    Can someone please help me write code for a poll. I have written most of the code but I think I have gone wrong somewhere and need someone to check it for me.

    I also don't understand how to link it my mysql database?

    Please help!

    Joe

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,373
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Show us what you have so far.

    Also show us where you're trying to link to database,
    but X-out any usernames and passwords.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Take a look at php.net/mysql_connect

    Then look at the mysql tutorials on tizag.com

    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    $init_connection
    =mysql_connect("localhost","xxxx""xxxx");
     
    mysql_select_db("Poll",$init_connection);

    function 
    displayPoll ($prompt$question) {
        echo 
    "<td><b>".$prompt"</b><form method=\"post\" action=\"\">
        <table border=\"0\" /"
    ;
        
        for(
    $i 0;$i count($questions);$i++){
            echo 
    "<tr><td><label><input type=\"radio\" name=\"poll\" value=\"".$i."\"/>".
    $questions[$i]."</label></td></tr>";
        }
        
        echo 
    "</table>
        <input type=\"submit\" name=\"submit\" value=\"Vote!\" />
        </form></td></td></table>"
    ;
    }
    $selection_one "SELECT * FROM Poll WHERE id = 1";
    $process_one mysql_query($selection_one);
    while(
    $row mysql_fetch_array($process_oneMYSQL_ASSOC)) {
        
    $mod_c_one = (int)$row['c_one'] + 1;
        
    $mod_c_two = (int)$row['c_two'] + 1;
        
    $mod_c_three = (int)$row['c_three'] + 1;
        
    $mod_c_four = (int)$row['c_four'] + 1;
    }

    $choices = array('Economics''Mathematics''Medicine''Veterinary Science''Law');
    echo 
    ",table><tr><td><b>Results:</b><table><tr><td>".($mod_c_one-1)."</td><td>".$choices[0]."</td></tr><tr><td>".($mod_c_two-1)."</td><td>".$choices[1]."</td></tr><tr><td>".($mod_c_three-1)."</td><td>".$choices[2]."</td></tr><tr><td>".($mod_c_four-1)."</td><td>".$choices[3]."</td></tr></tables></td>";
    displayPoll('Which University course will earn you the most money?'$choices);
    if(isset(
    $_POST['submit'})){
        if(isset(
    $_POST['Poll'}))
            
    $selection $_POST['Poll'];
        else
            
    $selection "";
            
        if(
    strlen($selection) > 0){
            switch((int)
    $selection){
                case 
    0:
                    
    $select_stmt "UPDATE poll SET c_one = $mod_c_one WHERE id = 1";
                    break;
                case 
    1:
                    
    $select_stmt "UPDATE poll SET c_two = $mod_c_two WHERE id = 1";
                    break;
                case 
    2:
                    
    $select_stmt "UPDATE poll SET c_three = $mod_c_three WHERE id = 1";
                    break;
                case 
    3:
                    
    $select_stmt "UPDATE poll SET c_four = $mod_c_four WHERE id = 1";
                    break;
            }
            
            
    $process mysql_query($selection_stmt) or die(mysql_error());
            if(
    $process)
                echo 
    "Your vote has been casted => ";
            else
                echo 
    "Error";
                
            echo 
    $choices[$selection];
            
        }else
            echo 
    "You made no choices!";
        

    ?>
    Last edited by codeseeker; 01-07-2012 at 12:18 AM.

  • #5
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,373
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Explain what is not happening, or errors you're getting.

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Explain what is not happening, or errors you're getting.
    I am getting a syntax error on line 30, not sure why. I am also unsure of how to view this poll or where to place it in my html?

    Bit new to all this

  • #7
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,373
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Let's deal with the syntax first ...

    Check all of these, where you used a } instead of ]

    if(isset($_POST['submit'})){
    if(isset($_POST['Poll'}))

    should be [...]

    make sure you don't have any more of them like that.

  • #8
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,373
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Also, what is the poll supposed to do?
    Is someone supposed to pick one from a list?
    What are you polling?

  • #9
    New Coder
    Join Date
    Jan 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Also, what is the poll supposed to do?
    Is someone supposed to pick one from a list?
    What are you polling?
    Ah thank you! Thats the problem.

    Well I want someone to be able to chose an option and then be shown instant results as a percentage on the same page. How do I do this?

  • #10
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,373
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    First of all, I assume you're storing each item to poll in the database,
    and you also need to store the accumulative value and how many there are.
    It appears like you might be doing that ... am I correct?

    That's all you need to know ... $avg = total / how many

    Percent? Percent of what?

    Here's another error I found ... put single quotes around variables in your queries ...

    $select_stmt = "UPDATE poll SET c_one = '$mod_c_one' WHERE id = 1";


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •