home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 88 / PIWD88.iso / mac / contents / developer / tutorial_files / Pages90-91 / polladmin.php < prev   
PHP Script  |  2003-11-02  |  2KB  |  82 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. //if the form has been posted then insert the poll into the database
  16. if ($_POST['questionText'] != "") {
  17.     //insert question
  18.     $sql = "INSERT INTO tblQuestions (questionText) VALUES ('" . $_POST['questionText'] . "')";
  19.     if (!(mysql_query($sql, $conn)))
  20.     displayError();
  21.     
  22.     //get the id of the poll just inserted
  23.     $qID = mysql_insert_id();
  24.     
  25.     //insert the answers
  26.     $i = 1;
  27.     While ($i <= $_POST['acount']) {
  28.         $thisAnswer = $_POST{"questionAnswer" . $i};
  29.         if($thisAnswer != "") {
  30.             $sql = "INSERT INTO tblAnswers (answerText, questionID) VALUES ('" . $thisAnswer . "', " . $qID . ")";
  31.             if (!(mysql_query($sql, $conn)))
  32.             displayError();
  33.         }
  34.         $i++;
  35.     }
  36. }
  37. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  38.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  39.  
  40. <html>
  41. <head>
  42. <title>Poll Admin</title>
  43. <script language="javascript" type="text/javascript" src="functions.js"></script>
  44. </head>
  45.  
  46. <body>
  47. <h1>Poll Admin</h1>
  48. <?php
  49. if ($qID != "") {
  50.     print "<p>Your Poll has been created with an ID of <strong>" . $qID . "</strong></p>";
  51. }
  52. ?>
  53. <p>The following polls have already been created</p>
  54. <table>
  55. <tr><th>Question</th><th>ID</th></tr>
  56. <?php
  57. if (!($result = @ mysql_query("SELECT * FROM tblQuestions", $conn)))
  58. displayError();
  59.  
  60. while ($row = mysql_fetch_array($result)) {
  61.     print "<tr><td>" . $row['questionText'] . "</td><td>" . $row['questionID'] . "</td></tr>\n";
  62. }
  63. ?>
  64. </table>
  65. <h2>Create a new poll</h2>
  66. <p>To create a new poll enter your poll question and the possible answers.</p>
  67. <form method="post" action="<?=$_SERVER['PHP_SELF']?>" onsubmit="ratifyNames('questionAnswer')">
  68.  
  69. <p>Poll Question:
  70. <br /><input type="text" name="questionText" style="width:240px;" /></p>
  71. <div id="qAnswers">
  72. <p>Poll Answers:</p>
  73. <p id="fieldTemplate"><input type="text" name="questionAnswer" style="width:240px;" /></p>
  74. </div>
  75. <a href="#" onclick="addName()">Add another answer</a>
  76. <p><input type="hidden" name="acount" id="acount" value="" /></p>
  77. <p><input type="submit" value="add poll" /></p>
  78. </form>
  79. </body>
  80. </html>
  81.  
  82.