0

I have setup a join query to show the username from the accounts table when displaying information from my contacts table because it was only showing the user

For example, say the contacts ID is 125, the link below will show ID 3 from the accounts table.

Should be update.php?id=125 Actually showing update.php?id=3

(There are actually only ID's 2,3 & 4 in the accounts table)

$stmt = $pdo->prepare('SELECT contacts.id, contacts.name, contacts.last_name, contacts.dob, contacts.mobile_number, contacts.status, contacts.mentor, contacts.image, accounts.username, accounts.id FROM contacts INNER JOIN accounts ON contacts.mentor=accounts.id');
$stmt->execute([]);
$contact = $stmt->fetchAll(PDO::FETCH_ASSOC);
                        <?php if($contact == null){
                            echo "<tr><td>No Record Found!</td></tr>";
                        }else{ foreach($contact as $info){ ?>
                        <tr>
                            <td><?php echo $info['name']; ?> <?php echo $info['last_name']; ?></td>
                            <td><?php echo $info['mobile_number']; ?></td>
                            <td><?php echo $info['status']; ?></td>
                            <td><?php echo $info['dob']; ?></td>
                            <td><?php echo $info['username']; ?></td>
                            <td><img src="<?php echo $info['image']; ?>" alt="" style="height: 30px; width:30px;"></td>
                            <td>
-->                             <a href="display.php?id=<?=$info['id']?>"><i class="fa fa-eye" aria-hidden="true"></i></a>
-->                             <a href="update.php?id=<?=$info['id']?>"><i class="fas fa-pencil-alt"></i></a>
-->                             <?php if ($_SESSION['role'] == 'Admin'): ?><a href="del.php?id=<?=$info['id']?>"><i class="fas fa-trash fa-xs"></i></a><?php endif; ?>
                            </td>
                        </tr>
Accounts table (sample)

id
usernamne

Contacts Table (Sample)
id
name
last_name
dob
mentor

Mentor is the column where the account ID gets stored.

I have tried adding the AND statement at the end of the code

AND accounts.id=contacts.id

This just stops the page from showing any information.

0 Answers0