Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-06-2012, 11:23 PM   PM User | #1
codeseeker
New Coder

 
Join Date: Jan 2012
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
codeseeker is an unknown quantity at this point
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
codeseeker is offline   Reply With Quote
Old 01-06-2012, 11:55 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 01-06-2012, 11:55 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Take a look at php.net/mysql_connect

Then look at the mysql tutorials on tizag.com

__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-07-2012, 12:14 AM   PM User | #4
codeseeker
New Coder

 
Join Date: Jan 2012
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
codeseeker is an unknown quantity at this point
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..
codeseeker is offline   Reply With Quote
Old 01-07-2012, 12:22 AM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Explain what is not happening, or errors you're getting.
mlseim is offline   Reply With Quote
Old 01-07-2012, 12:27 AM   PM User | #6
codeseeker
New Coder

 
Join Date: Jan 2012
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
codeseeker is an unknown quantity at this point
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
codeseeker is offline   Reply With Quote
Old 01-07-2012, 12:39 AM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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 is offline   Reply With Quote
Old 01-07-2012, 12:41 AM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Also, what is the poll supposed to do?
Is someone supposed to pick one from a list?
What are you polling?
mlseim is offline   Reply With Quote
Old 01-07-2012, 09:10 AM   PM User | #9
codeseeker
New Coder

 
Join Date: Jan 2012
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
codeseeker is an unknown quantity at this point
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?
codeseeker is offline   Reply With Quote
Old 01-08-2012, 12:19 AM   PM User | #10
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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";
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:51 PM.


Advertisement
Log in to turn off these ads.