简体   繁体   中英

adding php if condition in mysql query

i am trying to add an if condition to mysql query to only update a certain field if condition is met, here is my code below, but i keep getting this error

ERROR

{ "error": { "message":SQLSTATE[HY093]: Invalid parameter number: parameter was not defined } }

CODE

$ok = 1;

$sql = "UPDATE users SET
        fn  = :first,
        ln  = :last
        ";
    if($ok == 1){
        $sql .= ",phone = :phone";
    }

$sql .= "WHERE users.id = :id";

Keep space between your concatenation,

$ok = 1;

$sql = "UPDATE users SET
        fn  = :first,
        ln  = :last";
    if($ok == 1){
        $sql .= ", phone = :phone ";
    }

$sql .= " WHERE users.id = :id";

I think you are missing a space

$sql =
"UPDATE users SET
    fn  = :first,
    ln  = :last
    ,phone = :phoneWHERE users.id = :id"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM