home *** CD-ROM | disk | FTP | other *** search
Wrap
<?php //a function to give us useful error messages function displayError() { die("Error " . mysql_errno() . " - " . mysql_error()); } //connect to the database if (!($conn = @ mysql_connect("localhost","root","pwd"))) die("Could not connect!"); //choose the poll db if (!(mysql_select_db("dbPoll", $conn))) displayError(); //set a variable to say which poll we are loading $latest = mysql_query("SELECT questionID FROM tblQuestions ORDER BY questionID DESC LIMIT 1", $conn); $thisPoll = mysql_fetch_array($latest); $qID = $thisPoll['questionID']; //if the form has been posted then insert the results into the database if ($_POST['answerID'] != "") { //insert results $sql = "INSERT INTO tblResponse (questionID,answerID) VALUES (" . $_POST['questionID'] . ",". $_POST['answerID'] . ")"; if (!(mysql_query($sql, $conn))) displayError(); } //query the database for a list of categories if (!($result = @ mysql_query("SELECT * FROM tblQuestions, tblAnswers WHERE tblQuestions.questionID = tblAnswers.questionID AND tblQuestions.questionID = " . $qID, $conn))) displayError(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Poll</title> </head> <body> <form method="post" action="<?=$_SERVER['PHP_SELF']?>"> <?php $question = mysql_fetch_array($result); echo "<p><strong>" . $question['questionText'] . "</strong>"; echo "<input type=\"hidden\" name=\"questionID\" value=\"" . $question['questionID'] . "\" />\n"; mysql_data_seek($result,0); while ($row = mysql_fetch_array($result)) { print "<br /><input type=\"radio\" name=\"answerID\" value=\"" . $row['answerID'] . "\" />" . $row['answerText']; } ?> <p><input type="submit" name="btnVote" value="vote!" /></p> </form> <?php //how many people have viewed this survey? $votes = mysql_query("SELECT * FROM tblResponse WHERE questionID = " . $qID); $i = mysql_num_rows($votes); ?> <p><strong><?=$i?></strong> votes have been cast</p> </body> </html>