Jump to content

help creating a poll/getting numbers to display


music_lp90

Recommended Posts

Hi, I'm teaching myself php and mysql and I'm trying to make a simple poll, very basic. Here's what I've done so far:HTML form:<body><form method="post" action="update_poll.php"><input type="radio" name="answer_yes" value="1">yes</input><br /><input type="radio" name="answer_no" value="1">no</input><br /><input type="radio" name="answer_unsure" value="1">unsure</input><br /><input type="submit" value="submit"></form></body>php that inserts into MySQL:<?php//connect to database$con = mysql_connect("#####","#####","#######"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("#####", $con);$sql="INSERT INTO poll (yes, no, unsure)VALUES('$_POST[answer_yes]','$_POST[answer_no]','$_POST[answer_unsure]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo 'Poll submitted'?>php to display results:<?php $con = mysql_connect("######","#####","######"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("#####", $con); $yes = mysql_query("SELECT sum('yes') FROM `poll`"); while($sum_yes = mysql_fetch_assoc($yes)) { echo $sum_yes; }mysql_close($con);?>my table is set as follows:yes INT default 0no INT default 0unsure INT default 0id INT default 0 auto_increment primary indexWhat works: The form submits data properly into my tableWhat doesn't work: I cannot figure out how to grab the numbers from the database to use in calculations to determine percents. I realize I have not included any calculations for determining poll percentages, that is because I am first trying to figure out how to assign the sum of each answer to a variable. With what I currently have, it displays "array" in the browser as the result.My other problem is that I am not sure how to make the radio buttons only allow users to select one option. Right now, since they are named differently users can select all three if they want, but if I make them all have the same name, then I don't know how I can get the answers from them.Sorry this is kind of long, any help would be greatly appreciated, thanks!

Link to comment
Share on other sites

mysql_fetch_assoc returns an array, so that's why the word "array" gets printed when you try to print the array. It would be better to do it like this:

$yes = mysql_query("SELECT sum(yes) as num FROM `poll`");$sum_yes = mysql_fetch_assoc($yes);echo $sum_yes['num'];

You don't need the while loop, because it's only going to return one row.For the radio buttons, you need to give them all the same name and different values.<input type="radio" name="answer_1" value="yes">yes</input><br /><input type="radio" name="answer_1" value="no">no</input><br /><input type="radio" name="answer_1" value="unsure">unsure</input><br />

Link to comment
Share on other sites

Thanks for the response. I've ended up going about doing the poll in a different way and I've got it working well, except for one problem. I created the table differently in MySQL it looks like this now:id INT[11] not null default 0 auto_increment Primary Keyname VARCHAR[100] not nullanswer INT[11] not null default 0I created the name field so that I can use the table for many different polls. In order for it to work though, I need to be able to require the query to return only the results that match the name field that I specify and the answer that I specify. I can return the query properly for the answer but it won't work for the name field for some reason.here's the php that I'm returning the results with:

<?php$con = mysql_connect("myHost","myDB","Password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("myDB", $con);$yes = mysql_query("SELECT * FROM answers WHERE answer = 1 AND name = 'testing'");$no = mysql_query("SELECT * FROM answers WHERE answer = 2 AND name = 'testing'");$unsure = mysql_query("SELECT * FROM answers WHERE answer = 3 AND name = 'testing'");$row1 = mysql_num_rows($yes);$row2 = mysql_num_rows($no);$row3 = mysql_num_rows($unsure);$row1_percent = $row1/($row1 + $row2 + $row3) * 100;$row2_percent = $row2/($row1 + $row2 + $row3) * 100;$row3_percent = $row3/($row1 + $row2 + $row3) * 100;echo "People who said yes" . " " . number_format($row1_percent, 2) . "%";echo "<br />";echo "People who said no" . " " . number_format($row2_percent, 2) . "%";echo "<br />";echo "People who were unsure" . " " . number_format($row3_percent, 2) . "%";echo "<br />";echo "Total votes: " . ($row1 + $row2 + $row3);?>

Like I said, it works when I remove the AND name = 'testing', but then I can't use the same table for multiple polls. Thanks for any help. Also, let me know if you need to see the code for the form or the php that inserts the form data into MySQL.

Link to comment
Share on other sites

If it is not returning any rows when you add an extra WHERE condition, then there are no rows matching what you wrote. Dump the database and see what is in the table, or try the query directly on the database using something like phpMyAdmin to execute the query. If the query returns rows for one set of conditions, and does not return rows for another set of conditions, then clearly the rows do not match the second set of conditions. If there were matching rows, they would be returned.

Link to comment
Share on other sites

Thanks, I changed the way I submit the data into the table and it works now. I'm not sure exactly what I was doing wrong before, but it works now and I've got the poll just about finished. I just need to learn how to add a cookie to it to check if a user has already voted, but I think I can figure that out with some tutorials I've seen about cookies.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...