我在使用isset()
函数处理从数据库中提取的输入值的PHP表单时遇到问题。当我使用以下代码时,isset函数未向编辑客户端表单上的输入字段返回任何内容。我需要一些解决方法来解决此问题。
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;
}
}
答案 0 :(得分:1)
尝试此操作,我假设您只有一个结果,因此返回第一个结果:
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;
}
}
之后,再次检查var_dump($ client)以查看您的数据。同样,当client为false时,也找不到该client。调整代码以检查$ client是否仍然为空。