Reputation: 9880
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.
Upvotes: 0
Views: 3424
Reputation: 677
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
Upvotes: 1