Resource id #6 error in PHP with MySQL

2019-03-06 03:35发布

问题:

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?

回答1:

$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.



回答2:

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.