home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 88 / PIWD88.iso / mac / contents / developer / tutorial_files / Pages90-91 / poll.php < prev    next >
PHP Script  |  2003-11-01  |  2KB  |  64 lines

  1. <?php
  2. //a function to give us useful error messages
  3. function displayError()
  4. {
  5.     die("Error " . mysql_errno() . " - " . mysql_error());
  6. }
  7.  
  8. //connect to the database
  9. if (!($conn = @ mysql_connect("localhost","root","pwd")))
  10. die("Could not connect!");
  11. //choose the poll db
  12. if (!(mysql_select_db("dbPoll", $conn)))
  13. displayError();
  14.  
  15. //set a variable to say which poll we are loading
  16. $latest = mysql_query("SELECT questionID FROM tblQuestions ORDER BY questionID DESC LIMIT 1", $conn);
  17. $thisPoll = mysql_fetch_array($latest);
  18. $qID = $thisPoll['questionID'];
  19.  
  20. //if the form has been posted then insert the results into the database
  21. if ($_POST['answerID'] != "") {
  22.     //insert results
  23.     $sql = "INSERT INTO tblResponse (questionID,answerID) VALUES (" . $_POST['questionID'] . ",". $_POST['answerID'] . ")";
  24.     if (!(mysql_query($sql, $conn)))
  25.     displayError();
  26. }
  27.  
  28. //query the database for a list of categories
  29. if (!($result = @ mysql_query("SELECT * FROM tblQuestions, tblAnswers WHERE tblQuestions.questionID = tblAnswers.questionID AND tblQuestions.questionID = " . $qID, $conn)))
  30. displayError();
  31.     
  32.  
  33. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  34.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  35.  
  36. <html>
  37. <head>
  38. <title>Poll</title>
  39. </head>
  40.  
  41. <body>
  42. <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
  43. <?php
  44. $question = mysql_fetch_array($result);
  45. echo "<p><strong>" . $question['questionText'] . "</strong>";
  46. echo "<input type=\"hidden\" name=\"questionID\" value=\"" . $question['questionID'] . "\" />\n";
  47. mysql_data_seek($result,0);
  48. while ($row = mysql_fetch_array($result)) {
  49.     print "<br /><input type=\"radio\" name=\"answerID\" value=\"" . $row['answerID'] . "\" />" . $row['answerText'];
  50. }
  51. ?>
  52. <p><input type="submit" name="btnVote" value="vote!" /></p>
  53. </form>
  54. <?php
  55. //how many people have viewed this survey?
  56. $votes = mysql_query("SELECT * FROM tblResponse WHERE questionID = " . $qID);
  57. $i = mysql_num_rows($votes);
  58. ?>
  59. <p><strong><?=$i?></strong> votes have been cast</p>
  60.  
  61. </body>
  62. </html>
  63.  
  64.