douqingji3026 2017-08-02 21:13
浏览 74

使用PHP提交表单时,如何跳过空字段值?

I have a form that echoes values for a specific user from a MySQL database using PHP. I am trying to figure out how to allow the user to submit the form to update their user information but have the form skip any field they have no filled out.

Current update statement

if (!isset($_POST['btnLogin'])) {
    $db = DB();
    $stmt = "UPDATE users SET fName = :fName, 
                              lName = :lName, 
                              emailAddress = :emailAddress 
                              WHERE user_id = $user->user_id";
    $query = $db->prepare($stmt);
    $query->bindParam(':fName', $_POST['fName'], PDO::PARAM_STR);
    $query->bindParam(':lName', $_POST['lName'], PDO::PARAM_STR);
    $query->bindParam(':emailAddress', $_POST['emailAddress'], PDO::PARAM_STR);
    $query->execute();
};

Form echoing user information

<form class="form-horizontal" action="profile.php" method="post">
                <div class="form-group">
                    <label class="col-lg-3 control-label">First name:</label>
                    <div class="col-lg-8">
                        <input class="form-control" type="text" name="fName" placeholder="<?php echo $user->fName ?>"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-3 control-label">Last name:</label>
                    <div class="col-lg-8">
                        <input class="form-control" type="text" name="lName" placeholder="<?php echo $user->lName ?>">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-3 control-label">Email:</label>
                    <div class="col-lg-8">
                        <input class="form-control" type="email" name="emailAddress" placeholder="<?php echo $user->emailAddress?>">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Username:</label>
                    <div class="col-md-8" style="margin-top: 7px;">
                        <?php echo $user->username ?>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label"></label>
                    <div class="col-md-8">
                        <input class="btn btn-primary" name="btnUpdate" value="Save Changes" type="button">
                        <span></span>
                        <input class="btn btn-default" value="Cancel" type="reset">
                    </div>
                </div>
            </form>

Currently this doesnt appear to update the database at all. If I leave the form completely blank and submit it, the values that existed in the database are now empty. i.e., just blank columns.

I have been looking at other examples on how to do this but I cannot seem to figure this out. Any help would be appreciated.

Just to be sure I am in fact updating the correct user, I made sure that my $user->user_id statement is in fact returning the correct user_id from the database for the update.

UPDATE

Currently this is how I have the update statement / code

if(!empty(['btnUpdate'])) {
$stmt = "UPDATE users SET fName = IF(:fName = '', fName, :fName), 
                lName = IF(:lName = '', lName, :lName), 
                emailAddress = IF(:emailAddress = '', emailAddress, :emailAddress)
        WHERE user_id = $user->user_id";
$db = DB();
$query = $db->prepare($stmt);
$query->bindParam("fName", $fName, PDO::PARAM_STR);
$query->bindParam("lName", $lName, PDO::PARAM_STR);
$query->bindParam("emailAddress", $emailAddress, PDO::PARAM_STR);
$query->execute();
}

Using <?php var_dump($_POST) ?> returns 0 after submitting the form and I still end up with empty database columns

  • 写回答

2条回答 默认 最新

  • doudi2005 2017-08-02 21:40
    关注

    This isn't ignoring empty fields, but a more user friendly solution would be to stick the database values into each input's value attribute. That way, you don't have to skip the fields if they're blank, and a user won't get confused when editing their data. If you don't want them to be able to edit the value, just include the readonly attribute.

    If you don't want to do this, than you could either create a dynamic query or use an if statement to decide which value to use, the form's or the database's.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算