Jump to content

displaying a hyperlink to a MySQL database record


webguync

Recommended Posts

Hi,

 

I have a webpage which displays results of a form by pulling out the records and displaying in a HTML table based layout. What I want to do is have a page which will be a hyperlink that when clicked will display the record of the individual, instead of everyone on the same page. So if Joe Schmoe filled out the form, there would be  Joe Schmoe link which when clicked will only display his results. Need some help in how to do this. Here is my current code which displays all of the table records on one page.

 

<table id="results">
<tr><th>Candidate Name</th></tr>

<?php
ini_set("display_errors","1");
ERROR_REPORTING(E_ALL);
$conn = mysql_connect("localhost","uname","pw");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
  
if (!mysql_select_db("Internal")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT Responses.editor_name,Answer1,Answer2,Answer3,Answer4,Answer5,Answer6,Answer7,Answer8,Answer9,Answer10,Answer11,Answer12,submit_timestamp,login_timestamp,
TIMESTAMPDIFF(SECOND,submit_timestamp,login_timestamp) as cTime
FROM Responses
LEFT JOIN Editor_Candidates
USING (user_id) ";
       

$result = mysql_query($sql);



if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}


while ($row = mysql_fetch_assoc($result)) {
   echo "<tr><td class='name'>{$row['editor_name']}</td></tr>";
echo "<tr><td class='section'><strong>Section 1</strong></td></tr>";
   
   for ($i =1;$i<9;++$i) {
echo "<tr><td>{$row['Answer'.$i]}</td></tr>";
  }
echo "<tr><td class='section'><strong>Section 2</strong></td></tr>";

echo "<tr><td>{$row['Answer10']}</td></tr>";
echo "<tr><td class='section'><strong>Section 3</strong></td></tr>";

echo "<tr><td>{$row['Answer11']}</td></tr>";
echo "<tr><td class='section'><strong>Section 4</strong></td></tr>";

echo "<tr><td>{$row['Answer12']}</td></tr>";

echo "<td>" . date('F j, Y g:i:sa', strtotime($row["submit_timestamp"])) . "</td></tr>";
$formatted_completion_time = formatTime($row['cTime']);
  echo "<tr><th class='complete'>Completion Time:</th></tr><tr><td>\n";
  echo $formatted_completion_time;
  echo "</td></tr>";


}
mysql_free_result($result);



?>

</table>

Assuming you've selected the primary key in the query, pass the value of the record's key to the query in the next script via $_GET, then retrieve and display the associated record.

 

in your current script, first line in the while loop.

echo "<tr><td class='name'><a href=\"script_to_get_individual_record.php?user_id=" . $row['user_id'] . "\">" . $row['editor_name'] . "</a></td></tr>";

 

Then just process the $_GET var as you would any other.

 

if( isset($_GET['user_id'] {
     $id = (int) $_GET['user_id']; //should be an integer anyhow, so cast it as one
     $query = "SELECT field1, field2, field3 FROM table WHERE user_id = $id";
     $result = mysql_query($query);
etc., etc.

ok, just to clarify...this part

if( isset($_GET['user_id'] {
     $id = (int) $_GET['user_id']; //should be an integer anyhow, so cast it as one
     $query = "SELECT field1, field2, field3 FROM table WHERE user_id = $id";
     $result = mysql_query($query);
etc., etc.

 

would be what pulls the individual record?

 

and I would change


echo "<tr><td class='name'>{$row['editor_name']}</td></tr>";

 

to this?

 

echo "<tr><td class='name'><a href=\"script_to_get_individual_record.php?user_id=" . $row['user_id'] . "\">" . $row['editor_name'] . "</a></td></tr>";

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.