...

View Full Version : Poll PHP



codeseeker
01-06-2012, 11:23 PM
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

mlseim
01-06-2012, 11:55 PM
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.

tangoforce
01-06-2012, 11:55 PM
Take a look at php.net/mysql_connect

Then look at the mysql tutorials on tizag.com

:thumbsup:

codeseeker
01-07-2012, 12:14 AM
<?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_one, MYSQL_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!";


?>

mlseim
01-07-2012, 12:22 AM
Explain what is not happening, or errors you're getting.

codeseeker
01-07-2012, 12:27 AM
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

mlseim
01-07-2012, 12:39 AM
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.

mlseim
01-07-2012, 12:41 AM
Also, what is the poll supposed to do?
Is someone supposed to pick one from a list?
What are you polling?

codeseeker
01-07-2012, 09:10 AM
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?

mlseim
01-08-2012, 12:19 AM
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";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum