Apr
25th

How To Create Basic Poll Using PHP & MySQL (Part 2)

Files under PHP & MySQL | 2 Comments

This week, I will teach you how to write a class for poll. Basically, you need to write static function to:

- query your poll question id, question, answer, option count, total vote count and status from tbl_poll
- query user voted record to check whether user has voted before or not.
- Do delete record from tbl_user_vote and insert user_id, qst_id and option value if he has voted.
- Update option count and total vote count.

poll

You can create php file named Poll.php

class Poll {

public static function GetPollQuestion() {

$sql = InfoDb::query(
‘SELECT qst_id, qst, ans1, ans2, ans3, ans4, opt1, opt2, opt3, opt4, votes, status FROM ‘ //the field name is case insensitive, e.g, you can put Qst_id, Qst….
.tbl_poll’ //make sure this table name is same with your table name set in database
.’ WHERE status=1′,
NULL,
NULL
);

$row = $sql->fetchRow();
return self::GetPollQuestionRowObject($row);

}
public static function GetUserVote
($qst_id,$userid) {

$sql = InfoDb::query(
SELECT user_id, qst_id, `option` FROM ‘ //you must use correct escape character for the word option, use this “ in stead of ‘’
.’tbl_user_vote’
.’ WHERE qst_id=? and user_id=?’,
//set to ? is to prevent sql injection
array(integer’,integer‘),
array($qst_id, $userid)
);

$row = $sql->fetchRow();
return self::GetUserVoteRowObject($row);

}
public static function UserVoteOption(
$userid, $qst_id, $option) {

InfoDb::dml(
“DELETE FROM tbl_user_vote”
.” WHERE qst_id=? and user_id=?”,

array(integer’,‘integer’),
array($qst_id,$userid)

);

return InfoDb::dml(
“INSERT INTO tbl_user_vote”
.” (user_id, qst_id, `option`)”
.” VALUES (?,?,?)”,

array(‘integer’,‘integer’,‘integer’),
array($userid, $qst_id, $option)
);

}
public static function IncreasePollAnswerCount($qst_id, $option) {

return InfoDb::dml(
“UPDATE tbl_poll”
.” SET opt$option=opt$option+1, votes=votes+1″
.” WHERE qst_id=?”,

array(‘integer’),
array($qst_id )
);

}

}

What Is SQL Injection?

(more…)

Apr
18th

How To Create Basic Poll Using PHP & MySQL (Part 1)

Files under PHP & MySQL | 1 Comment

This is the tutorial that will help you to create a basic poll, which is user log in based voting. In the other words, each user is entitled to vote for one time for that question. If the question changes, then they can vote again.

A lot of resources that I found from Internet are those teaching you how to create a poll but the voting is tracked by IP address. Each IP address is entitled to vote for that particular question. This is not the right way because they can just restart their modem (if they are using dynamic IP address) and try to vote again or if all employees in the company are using shared IP address, then only one people can vote.

Today I will let you know the fields that are required when creating polling table in MySQL. Next lesson only proceed to PHP coding.

MySQL: Tutorial On How To Create Polling Table?

You need to create 2 table, one is to store all the information about polling question and answer while the another one is to store the user voted option.

1. Create your table named tbl_poll.

Photobucket

i) You need to have question id, which uniquely identified each question. So, create the question id in your table. E.g, qst_ID, set the type to INT. Make is as your primary key.

ii) Next, you need another field for displaying the question. E.g, qst, set the type to TEXT.

iii) Create 4 fields in your table (assume the voting has 4 answer) to display the polling answers. E.g, ans1, ans2, ans3, ans4, set the type to TEXT.

iv) Next, create 4 option fields in your table to count the option (frequency of the option) that is chosen by users. I am going to teach you how to display the result in percentage value instead of number of people voted. E.g, opt1, opt2, opt3, opt4, set the type to INT.

v) Create 1 field to count the total number of 4 options (sum up the value of opt1, opt2, opt3, and opt4). E.g, votes, set the type to BIGINT.

vi) Next, create 1 field to set polling status (set it to 1 if you want the question to be displayed or else set it to 0). E.g, status, set the type to INT.

(more…)