【问题标题】:PHP If isset() Not Returning Value For HTML InputPHP If isset() 不返回 HTML 输入的值
【发布时间】:2019-03-13 02:48:27
【问题描述】:

我遇到了一个 PHP 表单问题,该表单使用 isset() 函数处理从数据库中提取的输入值。当我使用下面的代码时,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;
    }
}

【问题讨论】:

  • 请发var_dump($client)
  • if(isset($firstName)) echo firstName; 缺少 $
  • 你的错误报告一定不能开启:stackoverflow.com/questions/1053424/…
  • 好吧,你有它;)调试你的查询
  • 还检查 @apokryfos cmets 关于 firstName 缺少 $

标签: php forms function pdo


【解决方案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 仍然为空。

【讨论】:

  • 不幸的是,这仍然没有按预期工作。我已经尝试了 var_dump,但我仍然得到一个 0 的数组返回。非常感谢您对此的帮助。
  • 在我建议的更改之后 var_dump($client) 会说什么?
  • 我得到输出:bool(false)
  • 这意味着没有找到您的查询的客户。你确定request()-&gt;get('client_id') 总是提供一个ID 吗?试试var_dump(request()-&gt;get('client_id'));看看有没有clientId
  • 非常感谢您对这个 sietse85 的帮助,我设法让代码按预期启动并运行:D
猜你喜欢
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
相关资源
最近更新 更多