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 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体