PHP表单仅显示user_id是否在数据库中

时间:2018-01-08 04:33:03

标签: php forms

我正在开发一个PHP表单,用户可以在其中添加信息或更新以前的信息。 构建时我手动将一些信息输入数据库以使工作正常。现在我发现表单不会显示给新用户,因为数据库中还没有user_id。(我测试并手动添加了user_ids,然后显示了表单。)

我相信它,因为我的表单包含在while循环中。 如果用户还没有数据,如何更改我的表单(INPUTS)以填充?

<?php

$user_id = $_SESSION['User']['logged_in'];
$businessProfileQuery = $db->query("SELECT * FROM sellerBusinessProfile 
WHERE user_id = $user_id;") or die(mysqli_error($db));
while( $row = $businessProfileQuery->fetch_assoc() ) {
       $savedBusinessName = $row['businessName'];
       $$savedBusinessAddress = $row['businessAddress'];
?>
<form>
<div class="form-group row">
        <div class="col-lg-12">
            <p class="content-header">BUSINESS ADDRESS</p>
            <p class="seller-profile-text"></p>

            <input name="businessName" type="name" class="form-control 
                   seller-input" value="<?php echo $savedBusinessName; ?>">

            <input name="businessAddress" type="name" class="form-control"  
                   value="<?php echo $savedBusinessAddress; ?>" >
        </div>
</form>
<?php
 }
 ?>

2 个答案:

答案 0 :(得分:0)

您可以计算行数。

如果行数小于或等于0,则表示该用户没有数据集。

如果行数大于0,则表示该用户已有数据。

                <?php

                $user_id = $_SESSION['User']['logged_in'];
                $businessProfileQuery = $db->query("SELECT * FROM sellerBusinessProfile 
                WHERE user_id = $user_id;") or die(mysqli_error($db));
             if ($businessProfileQuery->num_rows <= 0) {
                ?>
                <form>
                <div class="form-group row">
                        <div class="col-lg-12">
                            <p class="content-header">BUSINESS ADDRESS</p>
                            <p class="seller-profile-text"></p>

                            <input name="businessName`enter code here`" type="name" class="form-control 
                                   seller-input" value="">

                            <input name="businessAddress" type="name" class="form-control"  
                                   value="" >
                        </div>
                </form>
                <?php
                 }
                 ?>

答案 1 :(得分:0)

希望能帮到你!

<?php

$user_id = $_SESSION['User']['logged_in'];
$businessProfileQuery = $db->query("SELECT * FROM sellerBusinessProfile 
WHERE user_id = $user_id;") or die(mysqli_error($db));
$savedBusinessName = "";
$savedBusinessAddress = "";
while( $row = $businessProfileQuery->fetch_assoc() ) {
       $savedBusinessName = $row['businessName'];
       $savedBusinessAddress = $row['businessAddress'];
}       
?>
<form>
<div class="form-group row">
        <div class="col-lg-12">
            <p class="content-header">BUSINESS ADDRESS</p>
            <p class="seller-profile-text"></p>

            <input name="businessName" type="name" class="form-control 
                   seller-input" value="<?php echo $savedBusinessName; ?>">

            <input name="businessAddress" type="name" class="form-control"  
                   value="<?php echo $savedBusinessAddress; ?>" >
        </div>
</form>