Chúng ta sẽ có những phần sau: Phần câu hỏi: PHP: mysql> create table question(qid INT(10) UNSIGNED NOT NULLAUTO_INCREMENT, -> qtitle VARCHAR(255) NOT NULL, -> qdate DATE NOT NULL DEFAULT '0000-00-00', -> PRIMARY KEY(qid)); Phần trả lời: PHP: mysql> create table answer(aid INT(10) UNSIGNED NOT NULLAUTO_INCREMENT, -> qid INT(10) UNSIGNED NOT NULL, -> atitle VARCHAR(255) NOT NULL, -> acount INT(10) NOT NULL DEFAULT '0', -> PRIMARY KEY(aid)); Nhập dữ liệu bằng CSDL: PHP: mysql> Insert into question(qtitle,qdate) values("Ban thay website the nao ?"," 2009-10-10"); mysql> Insert into answer(qid,atitle,acount) values("1","Nhin rat dep",0); mysql> Insert into answer(qid,atitle,acount) values("1","Nhin Dep",0); mysql> Insert into answer(qid,atitle,acount) values("1","Nhin Cung duoc",0); mysql> Insert into answer(qid,atitle,acount) values("1","Nhin qua xau",0); Trang bình chọn (poll.php): PHP: <?php $conn=mysql_connect("localhost","root","root") or die("can not connect database"); mysql_select_db("poll_exam",$conn); if(isset($_POST['ok'])) { $id=$_POST['answer']; $qid=$_GET['questionid']; $sql3="update answer set acount=acount + 1 where aid='".$id."'"; mysql_query($sql3); header("location: result.php?questionid=$qid"); exit(); } $sql="select * from question order by qid desc"; $query=mysql_query($sql); if(mysql_num_rows($query) > 0) { $row=mysql_fetch_array($query); $qid=$row[qid]; echo "<form action='poll.php?questionid=$qid' method='post' >"; echo "<h2>$row[qtitle]</h2>"; $sql2="select * from answer where qid='$qid' order by aid"; $query2=mysql_query($sql2); if(mysql_num_rows($query2) > 0) { while($row2=mysql_fetch_array($query2)){ echo "<input type=radio name=answer value=$row2[aid]>$row2[atitle]<br />"; } } echo "<input type='submit' name='ok' value='Binh Chon'>"; echo "<a href='result.php?questionid=$qid'>Xem Ket Qua</a>"; echo "</form>"; } ?> Trang kết quả (result.php): PHP: <?php $conn=mysql_connect("localhost","root","root") or die("can not connect database"); mysql_select_db("poll_exam",$conn); if(isset($_GET['questionid'])) { $qid=$_GET['questionid']; $sql="select * from question where qid='".$qid."'"; $query=mysql_query($sql); $row=mysql_fetch_array($query); echo "<h2>$row[qtitle]</h2>"; $sql2="select qid, SUM(acount) as total from answer group by qid having qid='".$qid."'"; $query2=mysql_query($sql2); $row2=mysql_fetch_array($query2); $total=$row2[total]; $sql3="select * from answer where qid='".$qid."' order by aid"; $query3=mysql_query($sql3); if(mysql_num_rows($query3) > 0){ while($row3=mysql_fetch_array($query3)){ $percent=round(($row3[acount]/$total)*100,2); echo "<h4 style='color:red; font:12px verdana; '>$row3[atitle] : $row3[acount] ($percent %)</h4>"; }} } ?> Chúc bạn thành công!