Jump to content

Unable to view clients in list that are assigned to my account ID


Go to solution Solved by wongle,

Recommended Posts

Hi there,

I am trying to view a list of customers that are currently assigned to my by my login ID (or a users login ID) in the database and view them in a list.

Nothing is displaying in the list.

Here is what I have written so far.

$stmt = $pdo->prepare('SELECT id, username,email,role FROM accounts WHERE id = ?');
$stmt->execute([$_SESSION['id']]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);

if(isset($_POST['submit'])){
  $stmt = $pdo->prepare('SELECT * FROM contacts where mentor = ?');
  $stmt->execute([$account['id']]);
  $mentor = $stmt->fetchAll(PDO::FETCH_ASSOC);
  
}

Table - contacts Column name - mentor - stores user ID from accounts table

Table - accounts Column name - id

Table to view clients

        <table class="table table-sm table-hover" id="myTable">
                <tr class="header">
                    <th scope="col">Name</th>
                    <th scope="col">Mobile</th>
                    <th scope="col">Date of Birth</th>
                    <th scope="col">Status</th>
                    <th scope="col">Photo</th>
                    <th>Action</th>
                </tr>
                <?php if($fullContactInfo == null){
                    echo "<tr><td>No Record Found!</td></tr>";
                }else{ foreach($mentor as $info){ ?>
                <tr>
                    <td><?php echo $info['name']; ?> <?php echo $info['last_name']; ?></td>
                    <td><?php echo $info['mobile_number']; ?></td>
                    <td><?php $date = $info['dob']; $bits = explode('-', $date); $date = $bits[2] . '/' . $bits[1] . '/' . $bits[0]; echo $date; ?></td>
                    <td><?php echo $info['status']; ?></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>
                    </td>
                </tr>
                <?php } } ?>
        </table>

I var_dump of $accounts shows the account information as required and a dump of $mentor shows NULL

Any help would gratefully be appreciated.

Cheers.

How do you know that the first query produces anything?  You don't check to see if it even ran!

Are 'id' and 'mentor' numeric values?  If not, they need quotes on them in the queries.

Where is $fullcontactinfo defined? I don't see it.

One should ALWAYS verify that a query has run and/or that it has produced any rows.  You are not doing it in the proper place.

If you study your second block of code you could easily write it better but staying in php and including the html in it instead of the reverse way you are doing it.  Might make it easier to find any errors you may have there.

 

1 hour ago, wongle said:

a dump of $mentor shows NULL

then $_POST['submit'] is likely not set and that block of code isn't running or you have some code that you didn't post that's setting it to null. do you have php's error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your system, so that php would help you by reporting and displaying all the errors it detects? you should be getting an undefined variable error for $mentor and another error at the foreach() statement about it being null.

next, a post method form is using when performing an action on the server, such as inserting, updating, deleting data, or sending an email, ... Yo should be using a get method form/link for searching for data to display.

  • Solution

Managed to fix this;

  $stmt = $pdo->prepare('SELECT * FROM contacts where mentor = ?');
  $stmt->execute([$account['id']]);
  $mentors = $stmt->fetchAll(PDO::FETCH_ASSOC);

 

Also defined $fullcontactinfo to $mentors correctly (hold head down in shame lol)

 

Thank you all for your help :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.