Mega Code Archive

 
Categories / Php / MySQL Database
 

Example voting script. Lets people enter suggestions and vote for

existing ones. <? if($vote && !$already_voted) SetCookie("already_voted","1");?> <html><head><title>Name the Baby Computer</title></head> <body bgcolor="#ffffff" text="#032F5B" link="#537492" vlink="#000000"> <FONT FACE="Verdana, Arial, Helvetica"> <h3>Name the new baby</h3> I will be getting a cute little <img src="tm.gif"> box to stick on my LAN at home soon. It needs a name. <b><i>something</i></b>.lerdorf.on.ca<p> Please give me some suggestions or vote for an existing one. </font> <form action="vote.php3" method="POST"> Suggestion: <input type=text name=new_name size=25 maxlength=29><P> <input type=submit value="Submit suggestion and/or vote"> <input type=reset><P> <? mysql_pconnect("localhost","",""); if($new_name) { if(!mysql_db_query("rasmus","insert into names values ('$new_name',0)")) { print mysql_errno().": ".mysql_error()."<BR>"; } } if($vote && $already_voted) { print "<font color=#ff0000>Hey, you voted already! Vote ignored.</font><P>\n"; } else if($vote) { if(!mysql_db_query("rasmus","update names set votes=votes+1 where name='$vote'")) { print mysql_errno().": ".mysql_error()."<BR>"; } } $result=mysql_db_query("rasmus","select sum(votes) as sum from names"); if($result) { $sum = (int) mysql_result($result,0,"sum"); mysql_free_result($result); } $result=mysql_db_query("rasmus","select * from names order by votes DESC"); print "<table border=0><tr><th>Vote</th><th>Suggestion</th><th colspan=2>Votes</th></tr>\n"; while($row=mysql_fetch_row($result)) { print "<tr><td align=center><input type=radio name=vote value=\"$row[0] \"></td><td>"; print $row[0]."</td><td align=right>".$row[1]."</td><td>"; if($sum && (int)$row[1]) { $per = (int)(100 * $row[1]/$sum); print "<img src=bline.gif height=12 width=$per> $per %</td>"; } print "</tr>\n"; } print "</table>\n"; mysql_free_result($result); ?> <input type=submit value="Submit suggestion and/or vote"> <input type=reset> </form> <a href="http://ca.php.net"><img align=top src="php3/logos/mini-logo1.gif" border=0></a> <tt><font size=-1><a href="nameit.phps">View Source</a></font></tt> </body></html>