duanbi3385 2014-04-01 12:35 采纳率: 100%
浏览 299
已采纳

在IF语句之后继续进程(无论条件如何),php

I have searched a lot but can't find my solution. I am new to php so I hope I can explain my question clearly.

I am trying to build a system that allows the user to update his/her information. There are some inputs in the form which are; username, email, old-new password...

What I want to achieve is updating only the changed inputs. (blank inputs will be ignored)

So I want the username input to be checked if it is blank and keep the process working regardless of the result if username is blank or not. By that way ı can update the table with only changed/typed inputs.

How can I achieve that?

if($username != "") {
    //update the username
}
// keep process going even if username condition is true.
if($email != "") {
    // update the email
}

PS: I didn't write the whole codes because there are at least 10 inputs and 3 selects. I tried lots of nested if,elseif,else statements so the codes I tried is so complicated and long.

I just wonder If there is a way to keep the process going after an "if statement" even if the condition is true.

UPDATE

I tried using just ifs, I was expecting the process will be continue but, for example;if I left blank the username and type the email, it updates the email.But if username input was typed and the email was typed; it just updates the username.

What could be the problem ?

  • 写回答

3条回答 默认 最新

  • dongxikuo5171 2014-04-01 12:46
    关注

    If all the data is updated on a single table say users, then you can generate the update command on the fly using the input data and finally execute the query as

    <?php
    $qry_update = "UPDATE `users` SET " ;
    if($username != ""){ 
        $qry_update .= "`username` = '$username', ";
    }
    if($email != ""){
     $qry_update .= "`email` = '$email', ";
    }
    ....
    ....
    
    $qry_update = rtrim($qry_update, ',');
    
    $qry = $qry_update." where idusers = $idusers "; 
    
    // Execute the query
    ?>
    

    The above is conventional way of doing it. But its better to use PDO with bind params.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用