user6779766 user6779766 - 2 months ago 7
MySQL Question

How to Get data that been clicked into next page in php?

I have two tables, one with id, questions and other with q_id, answers. i have linked both of them using the ID from question to q_id of answers. now i m trying to add a link to question, where if anyone clicks on the question it would go to the .php file where he can only see the selected question and its answer instead of all the Q&A in both tables.

Script 1::

`<?php`

$sql = "SELECT * FROM input ORDER BY date DESC";
`$result = $conn->query($sql);

if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++;`
<a href='answ.php?id = " . $row['id'] . "'><?php echo $row["question"]; ?> </a>


Script 2

`$sql = "SELECT * FROM input WHERE id =" .$_GET["id"];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++; `
`<B><big><font color= #ba4a00> Q:</font></big><?php echo $row["question"]; ?> </B>`


I m getting a error like..


Notice: Undefined index: id in C:\xampp\htdocs\the_database\answ.php
on line 549


where lin 549 is script2 line#1 and


Notice: Trying to get property of non-object in
C:\xampp\htdocs\the_database\answ.php on line 558


Any Help is Appreciated..

Answer

As display in first script, you are not getting any result of index: id, and you have passed parameter in URL. Now in second script you are using id as get parameter. But as you know you don't have any index named id, you can't get id from URL, and when you are trying to run the query there's a query error and it returns non-object.

replace following line:

<a href='answ.php?id = " . $row['id'] . "'><?php echo $row["question"]; ?> </a>

with:

<a href='answ.php?id=<?php echo $row['id']; ?>'><?php echo $row["question"]; ?> </a>