PHP Poll

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robukni
Forum Newbie
Posts: 4
Joined: Thu Apr 12, 2007 8:55 am

PHP Poll

Post by robukni »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi everyone I am Having problems with this poll script. It will not display anything on the page just blank

When i remove this line it will show the form but it will not add anything to the DB:

if(!($VoteData = mysql_fetch_array($result))) die(mysql_error());

Form Code:

Code: Select all

<?php

$dbhost = "localhost";
$dbname = "";
$dbuser = "";
$dbpass = "";

$link_id = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

$sql = "SELECT `Title`,`Question`,`ID` FROM `poll_data` ORDER BY `ID` DESC LIMIT 1";
if(!($result = mysql_query($sql))) die(mysql_error());
$PollData = mysql_fetch_array($result);

$sql = "SELECT `Option1`,`Option2`,`Option3`,`Votes` FROM `poll_data` WHERE `ID` = '".$PollData['ID']."' ORDER BY `ID` DESC LIMIT 1";
if(!($result = mysql_query($sql))) die(mysql_error());
if(!($VoteData = mysql_fetch_array($result))) die(mysql_error());

if($VoteData["Option1"] != 0) {
    $VotePercent1 = Round(($VoteData["Option1"] / $VoteData["Votes"]) * 100) . "%";
} else {
    $VotePercent1 = 0 ."%";
}
if($VoteData["Option2"] != 0) {
    $VotePercent2 = Round(($VoteData["Option2"] / $VoteData["Votes"]) * 100) . "%";
} else {
    $VotePercent2 = 0 ."%";
}
if($VoteData["Option3"] != 0) {
    $VotePercent3 = Round(($VoteData["Option3"] / $VoteData["Votes"]) * 100) . "%";
} else {
    $VotePercent3 = 0 ."%";
}

?>

<html>
<head>>
<title>Basic Poll - Written by Adman</title>
</head>
<body>
<form  method="POST" action="poll.php">
  <table width="500" border="1" cellspacing="0" cellpadding="8">
    <tr>
      <td colspan="3"><b><?=$PollData['Title']?> - <?=$PollData['Question']?></b></td>
    </tr>
    <tr>
      <td width="35%">
        <input type="radio" name="Vote" value="Option1">
        Yes</td>
      <td width=60%>
          <img src="bar.gif" width="<?=$VotePercent1?>" height="20">
      </td>
      <td><?=$VoteData["Option1"]?> Votes</td>
    </tr>
    <tr>
      <td width="35%">
        <input type="radio" name="Vote" value="Option2">
        No </td>
      <td width=60%>
            <img src="bar.gif" width="<?=$VotePercent2?>" height="20">
      </td>
      <td><?=$VoteData["Option2"]?> Votes</td>
    </tr>
    <tr>
      <td width="35%">
        <input type="radio" name="Vote" value="Option3" >
        Not Sure</td>
      <td width="60%">
        <img src="bar.gif" width="<?=$VotePercent3?>" height="20">
      </td>
      <td><?=$VoteData["Option3"]?> Votes</td>
    </tr>
    <tr>
      <td colspan="3">
<center>
          <input type="submit" name="Submit" value="Vote">
        </center>
      </td>
    </tr>
  </table>
  </form>
  </body>
  </html>

Poll Code:

Code: Select all

<?php
if(empty($_POST["Vote"])) die("You did not enter your vote");

$dbhost = "localhost";
$dbname = "";
$dbuser = "";
$dbpass = ";

$link_id = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

$sql = "SELECT `Option1`,`Option2`,`Option3`,`Votes`,`ID` FROM `poll_data` ORDER BY `ID` DESC LIMIT 1";
if(!($result = mysql_query($sql))) die(mysql_error());
if(!($PollData = mysql_fetch_array($result))) die(mysql_error());

if($_POST["Vote"] == "Option1") {
$Votes1 = $PollData["Option1"] + 1;
$TotalVotes = $PollData["Votes"]+ 1;

$sql = "UPDATE `poll_data` SET `Option1`='$Votes1', `Votes`='$TotalVotes' WHERE `ID` = '". $PollData['ID'] . "' LIMIT 1";
if(!($result = mysql_query($sql))) die(mysql_error());
echo "Vote successful! <a href=\"index.php\">Back</a> to the poll.";
}
else if ($_POST["Vote"] == "Option2"){
$Votes2 = $PollData["Option2"] + 1;
$TotalVotes = $PollData["Votes"]+ 1;

$sql = "UPDATE `poll_data` SET `Option2`='$Votes2', `Votes`='$TotalVotes' WHERE `ID` = '". $PollData['ID'] . "' LIMIT 1";
if(!($result = mysql_query($sql))) die(mysql_error());
echo "Vote successful! <a href=\"index.php\">Back</a> to the poll.";
}
else {
$Votes3 = $PollData["Option3"] + 1;
$TotalVotes = $PollData["Votes"] + 1;

$sql = $sql = "UPDATE `poll_data` SET `Option3`='$Votes3', `Votes`= '$TotalVotes' WHERE `ID` = '". $PollData['ID'] . "' LIMIT 1";
if(!($result = mysql_query($sql))) die(mysql_error());
echo "Vote successful! <a href=\"index.php\">Back</a> to the poll.";
}



?>

What could be wrong??
Any easier way of creating a poll scrip?

Thanks
Robert


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If there were no records found matching your second query the page will stop execution.
robukni
Forum Newbie
Posts: 4
Joined: Thu Apr 12, 2007 8:55 am

Post by robukni »

ok, so could you tell me how i should fix this? Im fairly new to PHP


Thanks Robert
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Code: Select all

if(!($VoteData = mysql_fetch_array($result))) die(mysql_error());
As feyd was saying, mysql_fetch_array will return false if the result set is empty (no rows for the query). This will cause the script to quit (and it looks like it's present in both scripts)

Code: Select all

$dbpass = ";
This line should throw a parse error.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You need to add code such that if mysql_fetch_array() doesn't give you a record you can display the "0%" results.

What code that is, I will leave for you to figure out, but I will say that everything you should need is right in your script already.
robukni
Forum Newbie
Posts: 4
Joined: Thu Apr 12, 2007 8:55 am

Post by robukni »

Ok thanks everyone i got it working.

One last question, how do i prevent people from voting more than once?

Im thinking some sort of session variable or something.


Thanks
Robert
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

PHP sessions only persist as long as the user's browser session by default. If you already have a user system implemented, store that user's id along with vote record and check against existing records in the database for every vote cast. If not, it's not possible to definitively identify an internet user. The best you can do is set a cookie to identify the user, though the user can delete it at his or her whim. See setcookie()
robukni
Forum Newbie
Posts: 4
Joined: Thu Apr 12, 2007 8:55 am

Post by robukni »

So if i was to create a user login system that would work?

How would i go about comparing the vote against one in DB?


I can do most of the other code.


Thanks
Robert
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You compare the user id with the vote table's list of id's regarding that poll. If your database returns a record, they've voted.
Post Reply