Use PHP to make a survey

Source: Internet
Author: User
Tags php and php and mysql php example
If you have any questions please contact me: http://www.webjx.com web@webjx.com
Reprint please indicate the source

Today to tell you how to do a survey with PHP example, through this example you can learn how to use PHP and MySQL to achieve programming, this example is not difficult, I hope you can have a harvest.

First create a MYSQ
database table, the table contains the fields and settings as follows:

# Table structure for table Poll_data
#
CREATE TABLE Poll_data (
ID tinyint (4) not NULL auto_increment,
Option1 tinyint (4) Not NULL default ' 0 ',
Option2 tinyint (4) Not NULL default ' 0 ',
Option3 tinyint (4) Not NULL default ' 0 ',
Votes tinyint (4) Not NULL default ' 0 ',
Title varchar not NULL default ',
Question varchar not NULL default ',
PRIMARY KEY (ID)
) Type=myisam;
Once we've set up our database tables, we're going to start writing code to solve the problem. Usually we solve a problem and would like to break it down so that we can divide and conquer, so that the whole problem is solved. Let's use this method to write program code.
The main functions of this survey are:
1. Display the title of the survey and the question of the investigation.
2. Displays the title of each voting item, the percentage of the total vote, and the percentage of the vote for each item.
3. Create a program file that allows you to vote in an investigation.
Now let's break down the problem:
1. Display the title of the survey and the question of the investigation.
A. Connecting the database code
B. Select the most recent record from the database.
2. Displays the title of each voting item, the percentage of the total vote, and the percentage of the vote for each item.
A. The number of votes can be selected for each project and the total number of votes can be displayed in the survey.
B. Division of votes and totals can draw a percentage.
3. Create a program file that allows you to vote in an investigation.
A. Results to be updated after a user's vote

We use a number of variables to help us connect to the database. The code is as follows:
$dbhost = "localhost";
$dbname = "Yourdatabase";
$dbuser = "YourUserName";
$dbpass = "Yourpass";

And then we start connecting to the database:
$link_id = mysql_connect ($dbhost, $dbuser, $dbpass);
mysql_select_db ($dbname);
Now what we do is specifically about the content of the database, requiring the database to automatically increase the record, and the program takes out the latest records, ID can automatically ascending, the database code is as follows:

$sql = "Select ' Title ', ' question ', ' id ' from ' poll_data ' ORDER by ' id ' DESC LIMIT 1";
if (!) ( $result = mysql_query ($sql)) Die (Mysql_error ());
$polldata = mysql_fetch_array ($result)

Below we will specifically select the voting items and calculate the specific percentages!

$sql = "Select ' Option1 ', ' Option2 ', ' Option3 ', ' votes ' from ' poll_data ' WHERE ' ID ' = '". $polldata[' ID ']. "' ORDER BY ' ID ' DESC LIMIT 1";
if (!) ( $result = mysql_query ($sql)) Die (Mysql_error ());
if (!) ( $votedata = mysql_fetch_array ($result)) Die (Mysql_error ());

Now I come to the specific percentage of the code, of course, our percentages in order to display a more specific image, you can display graphics to replace!
if ($votedata["Option1"]!= 0) {
$votepercent1 = Round ($votedata["Option1"/$votedata["votes"]) * 100). "%";
} else {
$votepercent1 = 0. " %";
}
if ($votedata["Option2"]!= 0) {
$votepercent2 = Round ($votedata["Option2"/$votedata["votes"]) * 100). "%";
} else {
$votepercent2 = 0. " %";
}
if ($votedata["Option3"]!= 0) {
$votepercent3 = Round ($votedata["Option3"/$votedata["votes"]) * 100). "%";
} else {
$votepercent3 = 0. " %";
}

We do this by making a picture of any color with a 20 pixel height of 1 pixels wide. We set up code to represent percentages in images:
<title>basic Poll-written by webjx.com</title>
<body>
<form method= "POST" action= "vote.php" >
<table width= "border=" 1 "cellspacing=" 0 "cellpadding=" 8 ">
<tr>
&LT;TD colspan= "3" ><b><?=$polldata[' Title ']?>-<?=$polldata[' question ']?></b></td >
</tr>
<tr>
&LT;TD width= "35%" >
<input type= "Radio" name= "Vote" value= "Option1" >
Yes</td>
&LT;TD width=60%>
</td>
<td><?=$votedata["Option1"]?> votes</td>
</tr>
<tr>
&LT;TD width= "35%" >
<input type= "Radio" name= "Vote" value= "Option2" >
No </td>
&LT;TD width=60%>
</td>
<td><?=$votedata["Option2"]?> votes</td>
</tr>
<tr>
&LT;TD width= "35%" >
<input type= "Radio" name= "Vote" value= "Option3" >
Not sure</td>
&LT;TD width= "60%" >
</td> <td><?=$votedata["Option3"]?> votes</td>
</tr>
<tr>
&LT;TD colspan= "3" >
<center>
<input type= "Submit" name= "Submit" value= "Vote" >
</center>
</td>
</tr>
</table>
</form>
</body>

Now we're going to set the user's voting code, and when there is a vote, we automatically calculate the percentages and update the database, and we use a variable to receive the vote: $_post.

if (Empty ($_post["Vote"])) Die ("You did not enter your Vote");

We select the most recent record in the table.

$dbhost = "localhost";
$dbname = "Misc";
$dbuser = "root";
$dbpass = "trigger";

$link_id = mysql_connect ($dbhost, $dbuser, $dbpass);
mysql_select_db ($dbname);

$sql = "Select ' Option1 ', ' Option2 ', ' Option3 ', ' votes ', ' id ' from ' poll_data ' ORDER by ' id ' DESC LIMIT 1";
if (!) ( $result = mysql_query ($sql)) Die (Mysql_error ());
if (!) ( $polldata = mysql_fetch_array ($result)) Die (Mysql_error ());

We update the records in the datasheet. We set 1 to the current number of votes, and the total number of votes is also set to 1. We then use the program to implement the data submitted to update the records in the datasheet:

if ($_post["Vote"] = = "Option1") {
$votes1 = $polldata["Option1"] + 1;
$totalvotes = $polldata["votes"]+ 1;

$sql = "UPDATE ' poll_data ' SET ' Option1 ' = ' $votes1 ', ' votes ' = ' $totalvotes ' WHERE ' ID ' = '". $polldata[' ID ']. "' LIMIT 1";

if (!) ( $result = mysql_query ($sql)) Die (Mysql_error ());
echo "Vote successful! <a href=\ "Index.php\" >Back</a> to the poll. ";
}
else if ($_post["Vote"] = = "Option2") {
$votes2 = $polldata["Option2"] + 1;
$totalvotes = $polldata["votes"]+ 1;

$sql = "UPDATE ' poll_data ' SET ' Option2 ' = ' $votes2 ', ' votes ' = ' $totalvotes ' WHERE ' ID ' = '". $polldata[' ID ']. "' LIMIT 1";
if (!) ( $result = mysql_query ($sql)) Die (Mysql_error ());
echo "Vote successful! <a href=\ "Index.php\" >Back</a> to the poll. ";

}
else {
$votes3 = $polldata["Option3"] + 1;
$totalvotes = $polldata["votes"] + 1;

$sql = $sql = "UPDATE ' poll_data ' SET ' Option3 ' = ' $votes3 ', ' votes ' = ' $totalvotes ' WHERE ' ID ' = '". $polldata[' ID ']. "' LIMIT 1";
if (!) ( $result = mysql_query ($sql)) Die (Mysql_error ());
echo "Vote successful! <a href=\ "Index.php\" >Back</a> to the poll. ";

}

All right, the whole program is over, I hope you can learn some skills and knowledge when you look at this program. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.