Resource id #6 error in PHP with MySQL - php

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; ?>
Vote
</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?

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

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.

Related

How can I access a user's answer for my quiz on a different PHP page?

I currently have a simple quiz on my page quiz_v1.php with 3 possible answers and a submit button. The answer is posted to a separate page, quiz_dest.php, where it currently displays the value for the answer ID (A, B or C).
quiz_v1.php
<form action="quiz_dest.php" method="post" id="quiz">
<ol>
<li>
<!-- display question as heading -->
<?php
while ( $row = $result->fetch_assoc() ) {
echo "<h3>" . $row[ "question" ] . "</h3>";
}
?>
<!-- display answers as radio -->
<div>
<input type= "radio" name="question1_answers" id="question1_answers_A" value="A"/>
<label for="question1_answers_A">
<?php while($row = $result2->fetch_assoc()) {echo $row["choice_text"];}?>
</label>
</div>
<div>
<input type= "radio" name="question1_answers" id="question1_answers_B" value="B"/>
<label for="question1_answers_A">
<?php while($row = $result3->fetch_assoc()) {echo $row["choice_text"];}?>
</label>
</div>
<div>
<input type= "radio" name="question1_answers" id="question1_answers_C" value="C"/>
<label for="question1_answers_A">
<?php while($row = $result4->fetch_assoc()) {echo $row["choice_text"];}?>
</label>
</div>
</li>
</ol>
<input type="submit" value="Submit answer"/>
</form>
I use the SQL statements below to retrieve the information from my DB.
//queries
$sql = "SELECT question FROM Question WHERE question_id = '1'";
$result = $conn->query( $sql );
$sql2 = "SELECT choice_text FROM Question_choices WHERE choice_id = '1' AND question_id = '1'";
$result2 = $conn->query( $sql2 );
$sql3 = "SELECT choice_text FROM Question_choices WHERE choice_id = '2' AND question_id = '1'";
$result3 = $conn->query( $sql3 );
$sql4 = "SELECT choice_text FROM Question_choices WHERE choice_id = '3' AND question_id = '1'";
$result4 = $conn->query( $sql4 );
In quiz_dest.php
$answer1 = $_POST[ "question1_answers" ];
echo "$answer1";
I would like to be able to assign to a variable the user's choice_id, carry this over to quiz_dest and use a SQL query to compare the choice_ID with is_right_choice to see if it is the correct answer and then display a message to the user.
I can't quite wrap my head around how I could use $_POST and $_GET to achieve this so any help would be greatly appreciated.
I have also included a screenshot below of the table in my database.
Screenshot
Use the $_GET[] method. If you are using GET than the answer should appear in the URL if the answer goes to another PHP page. You can also try adding the answers to a database and implement some MySQL.

PHP form not send any $_POST value

Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="POST">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit">
</form>
</div>
</div>
</body>
</html>
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label
for='.$question_body.'>Yes</label>
<input type="checkbox" name="'.$option_a.'" value="'.$option_a.'">
<input type="hidden" name="'.$question_id.'" value="'.$question_id.'">
<hr>
';
}
}
}
?>
Answer.php
<?php
include 'conn.php';
if(isset($_POST['Submit'])){
if(isset($_POST['answer'])){
print_r($_POST);
}
}
?>
The value checkbox and hidden input did not send any value on the Answer.php page. It did not send any error or warning. 'Option_a' == 'Yes' value and 'question_id' == S1,S2,S3 and so on. Each 'question_id' have their own question and user have to tick if yes. So i want send these value on another page. I hope any of you guys can help me.
I can understand what you actually want to do but not sure what you are actually trying to achieve by saving the options as name and question_id as hidden input.
How i would approach this is saving the question_id as name for the options and i would suggest to use radial buttons if its an yes or no question. Anyways to solve your problem make the following changes to your question format in pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label>'.$question_body.'</label>
<input type="checkbox" name="'.$question_id.'" value="'.$option_a.'">
<input type="checkbox" name="'.$question_id.'" value="'.$option_b.'">
<hr>
';
}
}
}
?>
Now here each question having two options will have the same name corresponding to the question and values as the options. Now your Answer.php should look like
<?php
include 'conn.php';
if(isset($_POST['Submit'])){ // if submit is true
// if you want to print all answers then loop through the question id
// $_POST[$question_id] gives you the value of the answer
//i.e the option the user has chosen for that particular question
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
echo $_POST[$question_id];
}
}
}
?>
Hope this makes sense and helps. :)
check here for more info on checkboxes.

can i use while loop inside another while loop in php

I'm creating a comment system in my project. I want each posted question to have a comment. I was able to post the comment, but I am having a problem displaying all comment to it's respective answers. I was able to display only one row but not the row of the comment. I try to use a while loop nested inside the while that echo's each question, but it hangs. When I use if it only displays the first row of the comment of each question.
So my question is how can I display them all?
<div class="answer">
<?php
include 'db.php';
$sql = "select * from answers where questionrid IN(select id from question where id='$qid')";
$result = mysqli_query($con,$sql);
if($result){
while($ro = mysqli_fetch_assoc($result)){
?>
<div class="a_view">
<pre>
<?php
echo $ro["answer"];
?>
</pre>
</div>
<div class="ans_comment">
<?php
if($ro["id"]){
$id = $ro["id"];
$sqli = "SELECT * FROM comments WHERE answerid='$id'";
$query = mysqli_query($con,$sqli);
$row = mysqli_fetch_assoc($query);
$num = mysqli_num_rows($query);
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
}
?>
</div>
<div class="add"><div class="coment">add a comment</div> <div id="coment">
<form class="cform" method="post" action="acomment.php">
<textarea type="text" name="comment" class="tcomment" placeholder="add your comment here,your is
required to give correction or more information about the problem"></textarea><br><br>
<input type="hidden" value="<?php echo $ro["id"]; ?>" name="userid">
<input type="submit" value="Post your comment">
</form>
</div></div>
<?php
}
}else{
echo "no record";
}
?>
<?php
$con->close();
?>
This is the section that made it hang
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
when I use if, it only echos one row.
Instead of
while($row){
do it just like you're doing in the while loop above
while($row = mysqli_fetch_assoc($query)){
The way you have it now, $row is never changing, and therefore always evaluates to true, leaving you stuck in your loop.

Transfer Mysql info from a page to another page

I am working on a trivia concept for a website of mine. All the question data is stored on a mysql database. I use the following code for the user to submit the answer of the trivia which is randomized.
<html>
<title>Trivia</title>
<body>
<h1>Trivia</h1>
<?php
mysql_connect("localhost", "trivia", "<snip>") or die(mysql_error());
mysql_select_db("trivia") or die(mysql_error());
$query = 'SELECT * FROM questions ORDER BY RAND() LIMIT 1';
$data = mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 1")
or die(mysql_error());
$info = mysql_fetch_array( $data );
Print "<b>question:</b> ".$info['question'] . " ";
?>
<form action="result.php" method="post">
<input type="hidden" name="checkbox" value=<?php
$info ['correctoption'] ?>>
<?php
Print $info ['option1'] ?> <input type="radio" name="ans" value=<?php
$info ['option1'] ?> /><br />
<?php
Print $info ['option2'] ?> <input type="radio" name="ans" value=<?php
$info ['option2'] ?> /><br />
<?php
Print $info ['option3'] ?> <input type="radio" name="ans" value=<?php
$info ['option3'] ?> /><br />
<?php
Print $info ['option4'] ?> <input type="radio" name="ans" value=<?php $info ['option4'] ?> /><br />
<input type="submit" value="submit" />
</form>
</body>
</html>
However since the question is random I can't get figure out how to get it checked on the following page
<?php
mysql_connect("localhost", "trivia", "<snip>") or die(mysql_error());
mysql_select_db("trivia") or die(mysql_error());
$query = 'SELECT * FROM questions ORDER BY RAND() LIMIT 1';
$data = mysql_query("SELECT * FROM `questions` WHERE 1")
or die(mysql_error());
// puts the "friends" info into the $info array
$info = mysql_fetch_array( $data );
$correctoption = $_POST ['checkbox'];
$answer = $_POST['ans'];
if ($answer ==
$correctoption) {
echo 'You are Correct';
}
else {
echo 'You are Incorrect';
}
?>
I have been searching for an answer to this and haven't gotten anywhere.
Retain question id in a hidden input field:
<input type="hidden" name="questionNumber" id="questionNumber" value="12345" />
Example: http://www.tizag.com/htmlT/htmlhidden.php
This way, you know the user response & question which user answered on submit.
Hope this will help.
Hopefully your questions have id's in the database. Use the question id as a hidden input field in your form. On the second page then, you only need to select the question which has the question id:
$query = 'SELECT * FROM questions WHERE id=' .mysql_real_escape_string($_POST['id']). ' LIMIT 1';
Also, you shouldn't post the correct answer in the html, it will be very easy to figure out for the users!

How to read/send post data with php and hold a variable in it

I have this code in a loop in my code, The loop makes one submit button for every member found. I need each button to have the members name stored in it, in a way it can be sent though post when that button is clicked. Im not sure if this is possible with post but i was trying a way i do it with URLS. Does anyone know how to do this?
<input type="submit" value="Attack" name="Attack?name=<?php echo $Member_name; ?>" />
<?php
if(isset($_POST['Attack'])){
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
}
Here is the whole code i was trying to store it in a hidden form but it only grabs the last member found and wont get others.
<?php
$sql = "SELECT name, rank FROM users ORDER BY rank DESC"; // Searches the database for every one who has being last active in the last 5 minute
$query = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($query);
$i = 1;
while($row = mysql_fetch_object($query)) {
$Member_name = htmlspecialchars($row->name);
$Member_level = htmlspecialchars($row->rank);
?>
<td><?php echo $i; ?></td>
<td><?php echo $Member_name; ?></td><td><?php echo $Member_level; ?></td><td>
<input type="hidden" name="thename" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
</td>
<?
if($i != $count) { // this counts the amount of people that are online and display the results.
echo "</tr><tr>";
}
$i++;
}
?>
<?php
if(isset($_POST['Attack'])){
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_POST['thename'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$profile_id = htmlspecialchars($row->id);
$profile_userip = htmlspecialchars($row->userip);
$profile_name = htmlspecialchars($row->name);
$profile_money = htmlspecialchars($row->money);
$profile_gang = htmlspecialchars($row->gang);
$profile_exp = htmlspecialchars($row->exp);
$profile_profile = htmlspecialchars($row->profile);
$profile_rank = htmlspecialchars($row->rank);
$profile_health = htmlspecialchars($row->health);
$profile_defence = htmlspecialchars($row->defence);
$profile_stanima = htmlspecialchars($row->stanima);
?>
OK, assuming everything else is working ok, and you are retrieving data.
Change this:
<input type="hidden" name="thename" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
To this:
<form method="POST" action="">
<input type="hidden" name="name" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
</form>
And also in your PHP, change this line:
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";
To:
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_POST ['name'])."'";
This isn't the best way to do this, you will be generating loads of HTML elements depending how many users you have, but it should solve you problem (providing everything else is working and receiving data).
HTML 5 & Javascript would be perfect for this and is something you should look into.

Categories