0

I am trying to display couple of fields from sql table and want to add a link in each row that takes user to a page that shows all results for that specific table row.

Let me explain more: I have a MySql table that has the following fields: filename, intro_data, content_data, conclusion_data. I have created a php page that displays the list of all data for the filenames & intro_data fields in that sql table using this code:

//Query to select rows or data in table
$query= "SELECT filename,intro_data FROM file_data";  
$result=mysqli_query($connect, $query);

if (!$result)  
{  
  die('Error fetching results: ' . mysqli_error()); 
  exit();  
}

echo '<table border="1">'; // start a table tag in the HTML
//Storing the results in an Array
while ($row = mysqli_fetch_array($result))  //Creates a loop to loop through results
{  
    echo "<tr><td>" . $row['filename'] . '</td><td>' . $row['intro_data'] . '</td><td> <a href="full_table_row_results.php">More Info</a></td></tr>';  
}  
echo '</table>'; //Close the table in HTML

//Closing DB Connection
mysqli_close($connect); 

If you noticed in the above code, I have a "More Info" anchor tag that links to 'full_table_row_results.php'. In this page, I want it to show all the field results (filename, intro_data, content_data, conclusion_data) for that particular row. I know that I can query all results for a table like this:

$query= "SELECT * FROM file_data"; 

But how can I create the 'full_table_row_results.php' that queries all fields for that particular row that the user is clicking on? Since the row results are from an array, how do I know which row number in that array has the user clicked on? I am not sure how to code the More Info page.

I am stuck on this and not sure how to implement this.

1 Answer 1

1

One solution (as always, there's many other).

First, you need an id for each row in your table (if you do not have already one). With MySQL an auto_increment integer field does the job. Next, you need to get the id in your php code.

$query= "SELECT id, filename,intro_data FROM file_data";

Then you use it as a parameter when you link to your full_table_row_results.php script. The link will be:

echo '<a href="full_table_row_results.php?id=' . $row['id'] . '">' /* etc. */ ;

(Adapt it in your code, I did not copy all your code to easier readability). And in this last script you get access to this parameter with $_GET['id']. Then you can query your database for this one row only (with a WHERE clause).

Hope this help

Sign up to request clarification or add additional context in comments.

2 Comments

id seems like a good idea and then passing it through to php file should work I guess. I didnt create an id field in mysql table yet. I will create it now. I am assuming, if I am only displaying the results created by a particular user only (by matching the username field in table with that logged in user), I am guessing the sql id for those row shouldnt change (unlike array value) since its auto-incremented to all value added to that table. I will add the id field now and give it a try. Thank you so much Armage. I will post back once I have tested it out. :)
I just created the id field with int, not null, auto-increment settings and it worked! PERFECT! I was then able to pass through to the php file and fetch the results for that id in that second page. Thank you so much.. I was stuck on it and your suggestion worked like a charm and was also easy to implement. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.