-1

I'm having issues with a PHP form that is using the isset() function for input values that have been pulled from a database. When I use the below code, the isset function is returning nothing to the input fields on an edit client form. I'm in need of some help on how I can get this head scratching problem solved.

edit-client.php

<?php
require_once __DIR__ . '/inc/bootstrap.php';
require_once __DIR__ . '/inc/head.php';
require_once __DIR__ . '/inc/nav.php';

$client     = getClient(request()->get('client_id'));

$firstName  = $client['first_name'];
$lastName   = $client['last_name'];
$notes      = $client['notes'];
$buttonText = 'Update Client';
?>

<div class="container-fluid">
    <div class="row">
        <?php include __DIR__ . '/inc/sidebar-nav.php'; ?>

            <main role="main" class="col-md-9 ml-sm-auto mt-4 col-lg-10 px-4 main">
                <h1 class="h3 border-bottom pb-3 mb-4 text-primary">Edit Client</h1>

            <form method="post" action="/procedures/procedure-edit-client.php">

                <label for="first_name" class="text-muted">First Name</label>                    
                <input type="hidden" name="first_name" value="<?php if(isset($firstName)) echo $firstName; ?>">

                <label for="last_name" class="text-muted">Last Name</label>
                <input type="text" id="last_name" name="last_name" class="form-control" value="<?php if(isset($lastName)) echo $lastName; ?>" required>

                <label for="notes" class="text-muted">Notes</label>
                <textarea id="notes" name="notes" class="form-control" rows="10"><?php if(isset($firstName)) echo $firstName; ?></textarea>

                <button type="submit" class="btn btn-action btn-primary">
                    <?php
                        if(isset($buttonText)) echo $buttonText;
                        else echo 'Add New Client';
                    ?>
                </button>
            </form>
        </main>
    </div>
</div>

<?php require_once __DIR__ . '/inc/footer.php';

functions.php

function getClient($clientId) {
    global $db;

    try {
        $query = "SELECT * FROM client WHERE client_id = ?";

        $stmt = $db->prepare($query);
        $stmt->bindParam(1, $clientId);
        $stmt->execute();

        return $stmt->fetch(PDO::FETCH_ASSOC);
    } catch(\Exception $e) {
        throw $e;
    }
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
frontendstu
  • 103
  • 1
  • 11

1 Answers1

1

Try this i assume you only get one result so returning that first result:

function getClient($clientId) {
    global $db;

    try {
        $query = "SELECT * FROM client WHERE client_id = ?";

        $stmt = $db->prepare($query);
        $stmt->bindParam(1, $clientId);
        $stmt->execute();
        $result = $stmt->fetchAll(); 

        return (!empty($result)) ? $result[0] : false;

    } catch(\Exception $e) {
        throw $e;
    }
}

After that check you var_dump($client) again to see your data. Also when client is false, it could not find the client. Adjust your code to check for that also else $client is still empty.

sietse85
  • 1,488
  • 1
  • 10
  • 26
  • Unfortunately this is still not working as expected. I have tried a var_dump and I am still getting a array return of 0. I really appreciate your help on this. – frontendstu Oct 08 '18 at 13:14
  • wat does var_dump($client) say after the change i suggested? – sietse85 Oct 08 '18 at 13:17
  • I get the output: bool(false) – frontendstu Oct 08 '18 at 13:22
  • that means no client was found with your query. Are you sure `request()->get('client_id')` is always giving a id? Try `var_dump(request()->get('client_id'));` to see if there is a clientId – sietse85 Oct 08 '18 at 13:31
  • Thanks so much for your help with this sietse85, I managed to get the code up and running as expected :D – frontendstu Oct 10 '18 at 00:34