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条)

报告相同问题?