OK, I'm trying to make a poll. I want it to check a database of IPs so a person can only vote once. I want to have a results file that it reads in, displays and updates based on the person's vote.
Here's what I have so far:
I've commented a lot of it so hopefully that'll help. I feel like 90% of it works because:
1) it reads the current results in from the wbresults.txt file.
2) it checks the mysql database for the users ip address
3) if the ip is not present, it allows the person to vote and it increments the count
4) if the person has voted (i.e. the ip address is present in the database) it displays the results, but won't let them vote.
the problem: the 3 statements involving the updating of the wbresults.txt file.
you can try it out if you want, but warning, YOU'LL ONLY GET ONE SHOT!
http://www.brianellisrules.com/temp/vote.inc - the code I included in this message
http://www.brianellisrules.com/temp/wbresults.txt - the results file
moved - the script (with my default page header). If you go here, it'll allow you to vote, but once you do vote, 3 warnings pop up (and it increments the count/results). if you hit reload, the warnings go away and the old count (from the file) is displayed (because the ip address exists and it doesn't allow the person the option of voting).
This is long. My problem: writing to a file. I know, it's basic... but I've fought with this thing on and off all day and I figured I'd put this up before I went to sleep.
Thanks, youn's are the best.
Here's what I have so far:
PHP:
<?php
$pollname = wb;
//get the IP address
$ip = getenv("REMOTE_ADDR");
//connect to the database
$db = mysql_connect("localhost", "brian_vote", "password here");
mysql_select_db("brian_vote", $db);
//check to see if the IP exists
$grab="select * from winterbeard where ipaddy='$ip'";
$ipresult=mysql_query($grab) or die("select fails");
$ipstatus=mysql_num_rows($ipresult);
//access the results
$filename = $pollname . "results.txt";
$inf = fopen($filename, "r");
$results = fread($inf, filesize($filename));
//extract the info
while(!feof($inf))
{
$results .= fgets($inf, 1024);
}
fclose($inf);
//separate the info into yes/no
$votes = explode("\r\n", $results);
$yes = $votes[0];
$no = $votes[1];
//display current results (testing purposes)
if (!$vote) {
echo ("yes: $yes");
echo ("<br>");
echo ("no: $no");
}
//if the ip doesn't exist, display the poll options
if ($ipstatus==0)
{
//poll html
echo ("<p>
<form method=\"POST\" name=\"winterbeardvote\" action=\"$PHP_SELF\"> \n
<input type=\"radio\" name=\"vote\" value=\"yes\">Yes!<br> \n
<input type=\"radio\" name=\"vote\" value=\"no\">No!<br> \n
<input type=\"submit\" VALUE=\"Vote!\"> \n
</p>");
//when the person votes update the results
if ($vote)
{
//update the results
if ($vote=="yes")
{
$yes++;
}
else
{
$no++;
}
//update the file (all errors/warnings when I try it)
$outf=fopen($filename, "w+");
fwrite($outf, $yes, $no);
fclose($outf);
//add the ip to prevent multiple votes
$addy= "insert into winterbeard (ipaddy) values ('$ip')";
$insert = mysql_query($addy);
//display new results
echo ("<br>");
echo ("yes: $yes");
echo ("<br>");
echo ("no: $no");
echo ("<p>Thanks for voting!</p>");
}
}
//if the ip address does exist, don't display the poll
else
{
echo ("yes: $yes");
echo ("<br>");
echo ("no: $no");
echo("<p>Thanks for voting!</p>");
}
?>
1) it reads the current results in from the wbresults.txt file.
2) it checks the mysql database for the users ip address
3) if the ip is not present, it allows the person to vote and it increments the count
4) if the person has voted (i.e. the ip address is present in the database) it displays the results, but won't let them vote.
the problem: the 3 statements involving the updating of the wbresults.txt file.
you can try it out if you want, but warning, YOU'LL ONLY GET ONE SHOT!
http://www.brianellisrules.com/temp/vote.inc - the code I included in this message
http://www.brianellisrules.com/temp/wbresults.txt - the results file
moved - the script (with my default page header). If you go here, it'll allow you to vote, but once you do vote, 3 warnings pop up (and it increments the count/results). if you hit reload, the warnings go away and the old count (from the file) is displayed (because the ip address exists and it doesn't allow the person the option of voting).
This is long. My problem: writing to a file. I know, it's basic... but I've fought with this thing on and off all day and I figured I'd put this up before I went to sleep.
Thanks, youn's are the best.