cerberus478 Posted January 17, 2012 Share Posted January 17, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/255213-poll-not-working/ Share on other sites More sharing options...
laffin Posted January 17, 2012 Share Posted January 17, 2012 if (isset($_POST['go']) && $_POST['go']=='Vote') { <input type = "submit" name="Submit" value = "Submit"> What do you see wrong? Quote Link to comment https://forums.phpfreaks.com/topic/255213-poll-not-working/#findComment-1308509 Share on other sites More sharing options...
cerberus478 Posted January 17, 2012 Author Share Posted January 17, 2012 I changed to go into submit, hopefully that was the wrong thing, but it still isn't working Quote Link to comment https://forums.phpfreaks.com/topic/255213-poll-not-working/#findComment-1308510 Share on other sites More sharing options...
cerberus478 Posted January 18, 2012 Author Share Posted January 18, 2012 I did changes to the if (isset($_POST['go']) && $_POST['go']=='Vote') { and then I debugged it using p($_POST) and it shows that it's submitting but it's still not saving Quote Link to comment https://forums.phpfreaks.com/topic/255213-poll-not-working/#findComment-1308789 Share on other sites More sharing options...
Proletarian Posted January 18, 2012 Share Posted January 18, 2012 Change... echo '<form action = "polls" method = "post">'; ...to... echo '<form action = "polls.php" method = "post">'; ...and see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/255213-poll-not-working/#findComment-1308794 Share on other sites More sharing options...
cerberus478 Posted January 18, 2012 Author Share Posted January 18, 2012 No it doesn't Quote Link to comment https://forums.phpfreaks.com/topic/255213-poll-not-working/#findComment-1308796 Share on other sites More sharing options...
Proletarian Posted January 18, 2012 Share Posted January 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255213-poll-not-working/#findComment-1308798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.