Jump to content

poll not working


cerberus478

Recommended Posts

Hi I tried to make a poll system but it's not working, when you submit the option it doesn't get added to the database and I don't know what I'm doing wrong.

 

This is my polls.php

<?php 

if (isset($_POST['go']) && $_POST['go']=='Vote') { 
    if (!isset($_POST['choice']) || !isset($_POST['current_poll'])) { 
        $error = 'You did not select an answer'; 
    } 
     
    if (empty($_POST['choice']) || empty($_POST['current_poll'])) { 
        $error = 'You need to select an answer'; 
    } 
    else { 

         
        $sql ='UPDATE poll_answers SET votes = votes + 1 WHERE poll_id="'.$_POST['current_poll'].'" AND id="'.$_POST['choice'].'"'; 

         
        mysql_query ($sql) or die ('SQL error !'.$sql.'<br />'.mysql_error()); 


        mysql_close (); 

        $error = 'Thank you for your answer '; 
    } 
} 
?> 

<html> 
<head> 
</head> 

<body> 
<?php 

$sql = 'SELECT id, questions FROM polls'; 


$req = mysql_query ($sql) or die ('SQL error !<br />'.$sql.'<br />'.mysql_error()); 


$data = mysql_fetch_array ($req); 

$votes = mysql_num_rows($req); 

if ($votes == 0) { 
    echo 'No survey.'; 
} 
else { 
     
    mysql_free_result ($req); 

     
    echo stripslashes(htmlentities(trim($data['questions']))),'<br />'; 

     
    echo '<form action = "polls" method = "post">'; 

     
    $sql = 'SELECT id, answers FROM poll_answers WHERE poll_id="'.$data['id'].'"'; 

     
   $req = mysql_query($sql) or die('SQL Error !<br />'.$sql.'<br />'.mysql_error()); 

     
    while ($donnees = mysql_fetch_array($req)) { 
         
        echo '<input type="radio" name="choice" value="' , $donnees['id'] , '"> ' , stripslashes(htmlentities(trim($donnees['answers']))) , '<br />'; 
    } 
    ?> 
    <input type = "hidden" name = "current_poll" value = "<?php echo $data['id']; ?>"> 
    <input type = "submit" name="Submit" value = "Submit"> 
    </form> 
    <?php 
} 


mysql_free_result ($req); 


mysql_close (); 
?> 
<br /><br /> 
<a href="test">See the result</a> 
<?php 

if (isset($error)) echo '<br /><br />',$error; 
?> 
</body> 
</html>

 

and this is my test.php

<html> 
<head> 
</head> 

<body> 
<?php 


$sql = 'SELECT id, questions FROM polls ORDER BY id DESC LIMIT 0,1'; 


$req = mysql_query ($sql) or die ('error SQL !<br />'.$sql.'<br />'.mysql_error()); 


$data = mysql_fetch_array ($req); 



$votes = mysql_num_rows($req); 
mysql_free_result ($req); 

if ($votes == 0) { 
    echo 'No opinion poll.'; 
} 
else { 


    echo stripslashes(htmlentities(trim($data['questions']))),'<br />'; 


    $picture_responses = array(); 


    $picture_nb_reponses = array(); 


    $sql = 'SELECT answers, votes FROM poll_answers WHERE poll_id="'.$data['id'].'"'; 


    $req = mysql_query($sql) or die('error SQL !<br />'.$sql.'<br />'.mysql_error()); 


    while ($data = mysql_fetch_array($req)) { 

        $picture_responses[] = $data['answers']; 
        $picture_nb_reponses[] = $data['votes']; 
    } 


    mysql_free_result ($req); 


    mysql_close (); 


    $nb_reponses_of_opinion_poll = count ($picture_responses); 


    $nb_total_reponse = array_sum ($picture_nb_reponses); 


    if ($nb_total_reponse == 0) { 

        echo 'No vote '; 
    } 
    else { 

        for ($i = 0; $i < $nb_reponses_of_opinion_poll; $i++) { 

            echo $picture_responses[$i]; 


            $percentage = ($picture_nb_reponses[$i] * 100) / $nb_total_reponse; 


            $percentage = round ($percentage, 1); 


            echo ' ',$percentage,' %<br />'; 
        } 

     
        echo '<br /><br />Number of answers : ', $nb_total_reponse; 
    } 
} 
?> 
</body> 
</html>

Link to comment
Share on other sites

Are your mysql_query()'s returning TRUE or FALSE? Particularly, is the query with the UPDATE returning TRUE or FALSE?

 

It's hard to tell what you're trying to do with all the HTML and PHP mixed together like it is and without comments.

 

Try seperating the PHP into functions that can be called in the HTML so the HTML (presentation) isn't cluttered with PHP (execution).

 

And add comments that explain what you're trying to accomplish with each line.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.