home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Practical Internet Web Designer 88
/
PIWD88.iso
/
mac
/
contents
/
developer
/
tutorial_files
/
Pages90-91
/
polladmin.php
< prev
Wrap
PHP Script
|
2003-11-02
|
2KB
|
82 lines
<?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();
//if the form has been posted then insert the poll into the database
if ($_POST['questionText'] != "") {
//insert question
$sql = "INSERT INTO tblQuestions (questionText) VALUES ('" . $_POST['questionText'] . "')";
if (!(mysql_query($sql, $conn)))
displayError();
//get the id of the poll just inserted
$qID = mysql_insert_id();
//insert the answers
$i = 1;
While ($i <= $_POST['acount']) {
$thisAnswer = $_POST{"questionAnswer" . $i};
if($thisAnswer != "") {
$sql = "INSERT INTO tblAnswers (answerText, questionID) VALUES ('" . $thisAnswer . "', " . $qID . ")";
if (!(mysql_query($sql, $conn)))
displayError();
}
$i++;
}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Poll Admin</title>
<script language="javascript" type="text/javascript" src="functions.js"></script>
</head>
<body>
<h1>Poll Admin</h1>
<?php
if ($qID != "") {
print "<p>Your Poll has been created with an ID of <strong>" . $qID . "</strong></p>";
}
?>
<p>The following polls have already been created</p>
<table>
<tr><th>Question</th><th>ID</th></tr>
<?php
if (!($result = @ mysql_query("SELECT * FROM tblQuestions", $conn)))
displayError();
while ($row = mysql_fetch_array($result)) {
print "<tr><td>" . $row['questionText'] . "</td><td>" . $row['questionID'] . "</td></tr>\n";
}
?>
</table>
<h2>Create a new poll</h2>
<p>To create a new poll enter your poll question and the possible answers.</p>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>" onsubmit="ratifyNames('questionAnswer')">
<p>Poll Question:
<br /><input type="text" name="questionText" style="width:240px;" /></p>
<div id="qAnswers">
<p>Poll Answers:</p>
<p id="fieldTemplate"><input type="text" name="questionAnswer" style="width:240px;" /></p>
</div>
<a href="#" onclick="addName()">Add another answer</a>
<p><input type="hidden" name="acount" id="acount" value="" /></p>
<p><input type="submit" value="add poll" /></p>
</form>
</body>
</html>