Polls

User.php
<html>
<body>
<?php
include('config.php');
$connection = mysql_connect('localhost','root','')
or die('error connect!');
mysql_select_db($db) or die('error database!');


$query = "SELECT qid, qtitle FROM questions
ORDER BY qdate DESC LIMIT 0, 1";
$result = mysql_query($query)
or die("ERROR: $query.".mysql_error());
if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_object($result);
    $qid = $row->qid;
    echo $row->qtitle;
    echo "<form method = post action = 'user_submit.php'>";
    $query = "SELECT aid, atitle FROM answers WHERE qid = '$qid'";
    $result = mysql_query($query)
or die("ERROR: $query.".mysql_error());
    if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_object($result)) {
    echo "<input type = radio name = aid value = '".$row->aid."'>
                            '".$row->atitle."'</input><br />"; }
    echo "<input type = hidden name = qid value = '".$qid."'>";
    echo "<input type = submit name = submit value = 'Vote!'>";
    }
    echo '</form>';}
else {echo '<font size="-1">No questions</font>';}
mysql_close($connection);
echo "<form method = post action = 'view.php'>";
echo "<input type = submit name = submit value = 'Result'>";
?> </body> </html>

user_submit.php
<html>
<body>
<?php
if(isset($_POST['submit'])) {
    if (!isset($_POST['aid'])) {
        die('ERROR');
    }
include('config.php');
$connection = mysql_connect('localhost','root','')
or die('error connect!');
mysql_select_db($db) or die('error database!');
$query = "UPDATE answers SET acount = acount + 1
          WHERE aid = ".$_POST['aid']." AND qid = ".$_POST['qid'];
$result = mysql_query($query) or die("ERROR: $query. ".mysql_error());
mysql_close($connection);
    echo 'successfully';}
else {die('ERROR');

}  ?>  </body></html>

config.php
<?php
$host = 'localhost';
$user = 'root';
$db = 'db3';
?>

View.php
$result = mysql_query($query); //18 мөрөөс эхлэв
$row = mysql_fetch_object($result);
$total = $row->total;
    if ($total > 0) {
        unset($query);
        unset($result);
        unset($row);
$query = "SELECT atitle, acount FROM answers
WHERE qid = '".$_GET['qid']."'";
        $result = mysql_query($query);
        if (mysql_num_rows($result) > 0) {
            echo '<table border=1 cellspacing=0 cellpadding=15>';
            while($row = mysql_fetch_object($result)) {
                echo '<tr>';
                echo '<td>'.$row->atitle.'</td>';
                echo '<td>'.$row->acount.'</td>';
echo '<td>'.round(($row->acount/$total) * 100, 2).'%</td>';
                echo '</tr>';
            }
            echo '<tr>';
            echo '<td><u>TOTAL</u></td>';
            echo '<td>'.$total.'</td>';
            echo '<td>100%</td>';
            echo '</tr>';
            echo '</table>';
        }
    }
    else {
        echo 'No votes cast yet';
    }
    mysql_close($connection);
}
else {
    die('ERROR: Data not correctly submitted');
}
?>
</body>
</html>