0

I am this is for a polling system I am making, this code shows the user a list of questions they can pick from:

        <div class="main_questions">
            <p class="style1 style2"><strong>Select Your Question</strong></p>

<p class="style1">
            <form action="vote_list.php" method="post" name="form1" class="style1">
            <?php
                        $sql = "SELECT DISTINCT (question_tba) FROM question ORDER BY answer_id DESC";
                        $result = mysql_query($sql);
                        while($row = mysql_fetch_array($result)){
                            ?>
                            <p>
                                <?php echo $row['question_tba']; ?>
                                <input type="radio" name="questions" value="<?php echo $row['question_tba']; ?>">

                            </p>

                            <?php
                        }      
            ?>
            <input type="submit" name="submit" value="Vote">
            </p>
        </div>

This code should then post the chosen question to this page which allows them to cast a vote:

<?php

include('core/initialise.inc.php');

$q = $_POST['question_tba'];

if (isset($_GET['vote'], $_GET['id'])){
    add_vote($_GET['id'], $_GET['vote'], $q);
    echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=vote_logged.php\">";
}

?>

</div>
<div class="main_questions">
    <p class="style1 style2"><strong>Place Your Vote!</strong></p><?php
        foreach (get_answers($q) as $id => $answer){
            ?>
            <p>
            <?php echo $answer; ?>
            <a href="?vote=up&amp;id=<?php echo $id; ?>">Vote</a>
        </p>
        <?php
        }

        ?>
</div>

Then when they click on an answer to vote on the functions on this page should increment the chosen answers vote by 1 using these functions:

<?php

function get_answers($q){
    $q = $q;
    $sql = "SELECT answer_id, answer FROM question WHERE question_tba = '$q'";
    $result = mysql_query($sql);

    echo "$result";

    $answers = array();
    while (($row = mysql_fetch_assoc($result)) or die(mysql_error()))
    {                                                                         
        $answers[$row['answer_id']] = $row['answer'];
    }

    return $answers;
}

function add_vote($answer_id, $vote, $q2){
    $q2 = $q2;
    $answer_id = (int)$answer_id;
    $vote = '+';

    $sql = "UPDATE question SET answer_votes = answer_votes $vote 1 WHERE question_tba = $q2 AND answer_id = $answer_id";

    mysql_query($sql);

}

?>

However, my problem is that when I click on the question I would like to vote on, instead of displaying the answers that I can choose to vote from, it just displays Resource id #6. Can anyone tell me what is wrong with my code?

SJB
  • 661
  • 1
  • 8
  • 17
  • 1
    Welcome to Stack Overflow! As a side note, you are not doing any error checking in your query. If you don't do that, your script will break if the query fails. How to do proper error checking is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Mar 08 '12 at 13:49
  • Please specify which line exactly is outputting that text – Pekka Mar 08 '12 at 13:50

2 Answers2

3

$result is a resource returned by your mysql_query() call, not an actual row object/array. In the same way that you are using mysql_fetch_assoc() in other areas of your code to extract data, you will need to do this prior to your echo if you want to display the data.

jprofitt
  • 10,874
  • 4
  • 36
  • 46
  • What bit of the code are you referring to? Could you help me fix this problem, its quite urgent and I have never really used PHP. – SJB Mar 08 '12 at 14:03
  • The `echo "$result";` line. You should already have something you can use with the results from `get_answers()`. It looks like your code should be printing out the answers already though. – jprofitt Mar 08 '12 at 14:04
  • It was printing the results when I specified the SQL values, however, when I tried to use the posted variables it started displaying the Resource id #6. – SJB Mar 08 '12 at 14:11
  • In the first block of code I post 'question_tba' the next block then receives this and uses it as a parameter in get_answers($q) which puts this in the SQL query. – SJB Mar 08 '12 at 14:23
0

Okay, got this sorted out. Turns out, when posting a radio button value you have to use the name="" instead of value="" pretty easy solution.

SJB
  • 661
  • 1
  • 8
  • 17