I have a question which might sound a bit confusing but i’ll try my best to explain my problem.
I have 2 tables in my sql database: Table 1 is called Events, Table2 is called Results
The Events table contains entries about a past computer game match
eg. EventID EventName EventDate Location
1 GG vs AU 04/08 Game room 1
The Results table contains nearly the same entries.. but with more detail for that single entry
eg. EventID MemberID Score Ranking
1 02 7(victory):5 1st
Basically, I will go into the “Events” php file, and I will see the entries.
When I press any of the entries in the same row, (eg. 1, GGvsAU, 04/08, Gameroom1), itll link me to the Results Table which will give me a bit more indepth information about that match.
from that example, the eventID is 1 so when I press any of the entries in the same row, itll link me to results.php?EventID=1
The Events php file is working perfectly so its all good.. but once I press the entry, it links me to the Results php file.
What I have trouble right now is with the Results php file. When I press the entry in Events.php, it links me to results.php?EventID=1, but it shows me all the entries inside the database.. I want only the info for EventID1..
Also, would it be possible to “HIDE” the EventID and the “MemberID from view based on my codes below?
Events.php
<?
$pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
#Set Error Mode to ERRMODE_EXCEPTION.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $pdo->query('SELECT EventID, EventName, EventDate, Location from events');
$rowset = array();
if ($query) {
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
// Build array of rows
$rowset[] = $row;
}
// Output header first
$headrow = $rowset[0];
print("<table border=\"1\">\n<tr>\n");
// Use $rowset[0] to write the table heading
foreach ($headrow as $col => $val) {
printf("<th>%s</th>\n", $col);
}
print("</tr>");
// Then output table rows.
// Outer loop iterates over row
foreach ($rowset as $row) {
print("<tr>");
// Inner loop iterates over columns using $col => $val
foreach ($row as $col => $val) {
// We don't know your column names, but substitute the first column (the ID) for FIRSTCOL here
printf("<td><a href=\"listresults.php?EventID=%s\">%s</a></td>\n", $row['EventID'],$val);
}
print("</tr>");
}
}
print("</table>");
?>
Results.php (the one I have problem with)
<?
#Get the event id from $_GET
$int_event_id = $_GET["EventID"];
if((int)$int_event_id)
{
$pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
#Set Error Mode to ERRMODE_EXCEPTION.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $pdo->query('SELECT * from results WHERE EventID ='$int_event_id' ORDER By EventID ASC');
}
$rowset = array();
if ($query) {
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
// Build array of rows
$rowset[] = $row;
}
// Output header first
$headrow = $rowset[0];
print("<table border=\"1\">\n<tr>\n");
// Use $rowset[0] to write the table heading
foreach ($headrow as $col => $val) {
printf("<th>%s</th>\n", $col);
}
print("</tr>");
// Then output table rows.
// Outer loop iterates over row
foreach ($rowset as $row) {
print("<tr>");
// Inner loop iterates over columns using $col => $val
foreach ($row as $col => $val) {
// We don't know your column names, but substitute the first column (the ID) for FIRSTCOL here
printf("<td><a href=\"index.php?ID=%s\">%s</a></td>\n", $row['EventID'],$val);
}
print("</tr>");
}
}
print("</table>");
I really appreciate anyone who can help me with this problem!
Thanks very much!
Last Modified with verisimilitude’s Solution
Here you go. In your “Results.php”, you need to add a where clause to your query to display the data for the particular event id only. So check my below code.
<? #Get the event id from $_GET $int_event_id = $_GET[“EventID”]; if((int)$int_event_id) { $pdo = new PDO(‘mysql:host=localhost;dbname=clubresults’, ‘root’, ‘12345678’); #Set Error Mode to ERRMODE_EXCEPTION. $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);?>
Also you are linking with “listresults.php” in “events.php”, not sure if its a typo. It should be linked to “Results.php” right?