Adding the Survey Vote to the Database
This lesson is part of an ongoing Survey/Poll tutorial. The first part is here: Build your own Survey/Poll, along with all the files you need.
When the Vote button is clicked, the user will be sent to a new page. Behind the scenes, you're recording the vote and adding it the database.
As well as adding the vote to the database, you'll probably want to implement some sort of check to prevent people from voting over and over again. If you open up the code for the process.php page (in the survey folder), you'll see that the checking is done via a session variable. You met this code during the username/password walkthrough.
session_start();
if ( (isset($_SESSION['hasVoted'])) ) {
//Already Voted
}
else {
//Process the Vote
}
(NOTE: Session variables work by sending you an ID as a cookie. If you have cookies disabled then the session variable won't work, and you can vote over and over again! You might want to check that cookies are enabled in the browser.)
So we start a session, and check if the session variable called hasVoted has been set. If it has, then a vote from this user has already been added to the database. In which case, a message is displayed.
If no session has been set, then the else part of the if statement is executed. The first line of this is another if statement:
if ( isset($_GET['Submit1']) && isset($_GET['q']) ) {
}
Here's we're checking for two things: was the Submit button called Submit1 clicked on the previous page, and has the radio button data been handed over to this page (the radio button data will be in the variable called 'q')? If the answer to both questions is Yes then we can go ahead and process the data; if the answer is No, then we can assume that the button was clicked but the user didn't select a radio button. In which case, this message is displayed: print "You didn't selected a voting option!";
If all went OK, though, the first line of the new if statement to get executed is this:
$selected_radio = $_GET['q'];
This just gets which radio button was clicked. The value comes from the HTML form, and will be A, B or C. This is then placed into the variable called $selected_radio.
The next few lines open a connection to the database, like we did before. After the database has been successfully opened, we have this:
$_SESSION['hasVoted'] = '1';
This is our "hasVoted" session variable, and we're placing a value of "1" into it. If the user tries to vote again, a message of "You've already voted" will be displayed.
The next line is where we add the record to the answers table in our database:
$SQL = "UPDATE answers SET $selected_radio = $selected_radio + 1";
Here, we're using the SQL command UPDATE. After the word UPDATE, you type the name of the table you want to UPDATE (answers, for us). We only want to update the field that was passed to the page in the "q" variable. This will be either A, B or C, which are the column names from the answers table. We can then use the SET keyword to set just that column from the table. Because the variable called $selected_radio will contain only A, B or C we can just add 1 to whatever is currently there:
SET $selected_radio = $selected_radio + 1";
You'll notice that we're not making sure to move to the end of the records in the answers table (which we should do), but trusting MySQL to do it for us.
This is not a good idea! In your own code, you should make sure that the correct record is being updated. We're taking shortcuts for simplicity's sake.
After the table is updated, we close the connection to the database, and print out a message: "Thanks for voting".
And that's it - a vote has been added to the table.
You can add a "back" link to the HTML, or do anything else you like with your page. Or perhaps you could add a link so that the results of the voting can be viewed? We'll take a look at the code for the results page now.