Jump to content

Working with Poll Processing


Xtremer360

Recommended Posts

What I'm wanting to do for the UPDATE statement is have it update the current value of answer_votes for that answer in that form to have it increase its value + 1 and increase the value of totalVotes for the specific poll +1 but not sure how that's accomplished. My table scheme is below.

 

if (isset($_POST['vote_poll'])) {
    $poll_quetion_id = mysqli_real_escape_string($dbc, $_POST['poll_question_id']);
    $poll_answer_id = (int) $_POST['id'];
    $voter_ip_address = $_SERVER["REMOTE_ADDR"];
    $voter_ip_host = $_SERVER['HTTP_HOST'];
    	
    $query = "SELECT * FROM `poll_answer_votes` WHERE ((`voter_ip_addess` = '".$voter_ip_address."') AND (`voter_ip_host` = '".$voter_ip_host."'))";
    $Qresult = mysqli_query ( $dbc, $query ); // Run The Query
          
    if (mysqli_num_rows($Qresult) == 0) {
        $query = "INSERT INTO `poll_answer_votes` 
                (poll_question_id, poll_answer_id, voter_ip_address, voter_ip_host, vote_time_stamp) 
            VALUES 
                ('".$poll_question_id."','".$poll_answer_id."','".$voter_ip_addres."','".$voter_ip_host."', NOW())";
        mysqli_query($dbc, $query);

        $query = "UDPATE `poll_answers` SET ";
        mysqli_query($dbc, $query);
        
        $result = "good";
    } else {
        
    }
}

 

Table: polls

Fields: id, poll_question, total_votes

 

Table: poll_answers

Fields: id, poll_id, poll_answer, answer_votes

 

Table: poll_answer_votes

Fields: id, poll_question_id, poll_answer_id, voter_ip_address, voter_ip_host, vote_time_stamp.

 

Link to comment
Share on other sites

For some reason its not getting the id so that it can be used for the answerid after form submission.

 

 

if (isset($_POST['vote_poll'])) {
    $poll_question_id = mysqli_real_escape_string($dbc, $_POST['poll_question_id']);
    $poll_answer_id = (int) $_POST['id'];
    $voter_ip_address = $_SERVER["REMOTE_ADDR"];
    	
    $query = "SELECT * FROM `poll_answer_votes` WHERE (`voter_ip_address` = '".$voter_ip_address."')";
    $Qresult = mysqli_query ( $dbc, $query ); // Run The Query
          
    if (mysqli_num_rows($Qresult) == 0) {
        $query = "INSERT INTO `poll_answer_votes` 
                (poll_question_id, poll_answer_id, voter_ip_address, vote_time_stamp) 
            VALUES 
                ('".$poll_question_id."','".$poll_answer_id."','".$voter_ip_address."', NOW())";
        mysqli_query($dbc, $query);
        echo $query;

        $query = "UPDATE `poll_answers` SET answer_votes = answer_votes + 1 WHERE id = '".$poll_answer_id."'";
        mysqli_query($dbc, $query);
        echo $query;
        
        $query = "UPDATE `polls` SET total_votes = total_votes + 1 WHERE id = '".$poll_question_id."'";
        mysqli_query($dbc, $query);
        
        $result = "good";
    } else {
        $result = '';
        while ($row = mysqli_fetch_array($Qresult)) {
            if ($question        && $row['question']         == $question)      {$result .= 'bad1';}  
        }
    }
}

 

function getpoll($dbc) {
    $query = "
    SELECT
        polls.id as poll_id,
        polls.poll_question,
        polls.total_votes
    FROM
        polls
    WHERE
        polls.status_id = '1'
    ORDER BY
        polls.id DESC
    LIMIT 
        1";
    $result = mysqli_query($dbc, $query);
    if ( ($result) and (mysqli_num_rows($result) > 0) ) {
        $row = mysqli_fetch_array($result);
        $poll_id = $row['poll_id'];
        $poll_question = $row['poll_question'];
        $total_votes = $row['total_votes'];                
        $query = "
        SELECT
            poll_answers.id as answer_id,
            poll_answers.poll_answer
        FROM
            poll_answers
        WHERE
            poll_answers.poll_id = '".$poll_id."'";
        print "<form action=efedmanager/processes/polls.php method=post name=poll id=poll>";
        print "<p class=question>".$poll_question."</p>";
        $result = mysqli_query($dbc, $query);
        $num_rows = mysqli_num_rows($result);
        if ( ($result) and (mysqli_num_rows($result) > 0) ) {
            while ($row = mysqli_fetch_array($result)) {
                $fieldarray=array('answer_id','poll_answer');
    			foreach ($fieldarray as $fieldlabel) {
    				if (isset($row[$fieldlabel])) { 
    					$$fieldlabel=$row[$fieldlabel];
    					$$fieldlabel=cleanquerydata($$fieldlabel);
    				}
    			}
                print "<label>";
                print "<input type=radio name=poll value=".$poll_answer." id=".$answer_id." />".$poll_answer."";
                print "</label>";
            }
         }
        print "<input type=hidden name=poll_question_id value=".$poll_id." />";
        print "<input type=submit name=vote_poll id=vote_poll value=Vote />";
        print "</form>"; 
    } else {
      print "<p class=none>There are currently no open polls.";  
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.