I am currently working on a client management system for my Computing Coursework and I've come to the "Client Details" section, which allows staff and admin to view personal information about each client.

I've successfully coded the query and everything else around it, I'm just a but stuck on how to actually output the details of each client in a table.

The table will only contain details about a specific client, and so will contain only two columns and as many rows as many columns in the mysql table, like this:

Name | Mark
Surname | Kent
Address | 55 Street

The table has about 10 columns

etc

Here is my code:

$clientid = $_GET['client'];
$query = "SELECT * FROM client_details INNER JOIN users ON client_details.userID = users.id WHERE userID = $clientid";
$result = mysqli_query($dbcon,$query) or die('Error executing database query');
if (mysqli_num_rows($result) < 1) {
                    die('Error obtaining client details, no rows found. <a href="' . $_SERVER["HTTP_REFERER"] . '">Go Back</a>');
                }
while ($row = mysqli_fetch_array($result)) {
                    echo $row['firstname'] . '<br />';
                    echo $row['lastname'] . '<br />';                                       
                }
exit();

Dani AI

Generated

Nice — since already solved the basic output, here are a few practical refinements and safer patterns that make the two-column "field | value" display more robust and maintainable than a quick echo loop.

Keep labels separate from column names

  • Maintain an associative label map (column => human label) so you control wording and order without exposing raw DB names.
  • For special types (email, date, phone) format the value or wrap it (mailto for email) before HTML-encoding.

Escape output and validate input

  • Never print DB values directly into HTML: use htmlspecialchars when outputting text.
  • Validate the incoming client id (use filter_var or intval) and use prepared statements to avoid SQL injection (see mysqli::prepare).

Small example pattern (assumes you already fetched one associative row into $row):

$labels = ['firstname'=>'First Name','lastname'=>'Last Name','email'=>'Email Address','address'=>'Address'];

echo "<table class=\"client-details\">\n";
foreach ($labels as $col => $label) {
    $raw = isset($row[$col]) ? $row[$col] : null;
    if ($col === 'email' && filter_var($raw, FILTER_VALIDATE_EMAIL)) {
        $value = '<a href="mailto:'.htmlspecialchars($raw).'">'.htmlspecialchars($raw).'</a>';
    } else {
        $value = htmlspecialchars((string)$raw);
    }
    echo "<tr><th>".htmlspecialchars($label)."</th><td>$value</td></tr>\n";
}
echo "</table>\n";

Extras to consider

  • Use set_charset('utf8mb4') on the MySQL connection to avoid encoding issues.
  • Show a friendly placeholder for NULL/empty values.
  • If you prefer non-table markup for accessibility, a definition list (<dl>) is a good semantic alternative.
  • To avoid hard-coding labels you can store friendly names in column comments and read them via INFORMATION_SCHEMA / SHOW FULL COLUMNS (see MySQL docs for column metadata).

These tweaks keep the output clean, safe, and easier to extend for future fields or formatting needs.

Recommended Answers

All 4 Replies

Do you mean like this?

<?php
$clientid = mysql_real_escape_string($_GET['client']);
$query = "SELECT * FROM client_details INNER JOIN users ON client_details.userID = users.id WHERE userID = $clientid";
$result = mysqli_query($dbcon,$query) or die('Error executing database query');
if (mysqli_num_rows($result) < 1) {
    die('Error obtaining client details, no rows found. <a href="' . $_SERVER["HTTP_REFERER"] . '">Go Back</a>');
    }
echo '<table border=1 cellpadding=5 cellspacing=0>';
while ($row = mysqli_fetch_assoc($result)) {
    echo '<tr><td>'.$row['firstname'].'</td><td>'.$row['lastname'].'</td></tr>';
    }
echo '</table>';
exit();
?>

Yeah, thanks very much, what a quick reply :)

Actually sorry (my fault) that's not what I meant. I want PHP to output a two column table with the left column containing the actual field name in the mysql table and the right column containing the data for that client.

For example:

First Name | Joe
Second Name | Bloggs
Email Address |
Address | 10 Downing St

etc

Is this possible to do?

If you still don't understand what I am trying to do please let me know.

Ahh nevermind, I figured it out :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.