How to make multiple execute with PDO

-1

I have a very simple user update form, but I'm not able to successfully update using PDO and mysql.

Here is the snippet of code I'm having trouble with:

if (isset($_POST['ID'])){//Se informar o ID do usuário que deseja modificar
    $senha = strip_tags(sha1(md5(trim($_POST['senha']))));//criptografa a senha
    $ID = $_POST ['ID'];//recebe o ID informado pelo usuario para modificar no sql
    if (!empty($_POST['NEWID'])){//modifica o ID
        $sqlid = "UPDATE usuarios SET ID = :ID WHERE ID = $ID";
        $stmtid = $pdo->prepare($sqlid);
        $stmtid->bindParam(':ID', $_POST['NEWID'], PDO::PARAM_STR);
        $stmtid->execute();
    }
    if (!empty($_POST['usuario'])){//modifica o usuario
        $sqlusr = "UPDATE usuarios SET usuario = :usuario WHERE ID = $ID";
        $stmtusr = $pdo->prepare($sqlusr);
        $stmtusr->bindParam(':usuario', $_POST['usuario'], PDO::PARAM_STR);
        $stmtusr->execute();
    }
    if (!empty($_POST['senha'])){//modifica a senha
        $sqlpass = "UPDATE usuarios SET senha = :senha WHERE ID = $ID";
        $stmtpass = $pdo->prepare($sqlpass);
        $stmtpass->bindParam(':senha', $senha, PDO::PARAM_STR);
        $stmtpass->execute();
    }
}

The problem is that I can modify only 1 of these items at a time, if I want to modify the ID I can, but if I put it to modify the user also it will only modify the 1 item that in the case is the ID, my goal is to only make changes to the items that the user types ... I have already tried to do an execution only but the problem persisted ..

    
asked by anonymous 23.10.2017 / 13:01

1 answer

5

You do not have to make multiple UPDATE , you can only do one. It will save runtime because you will have fewer requests to the server:

if (isset($_POST['ID'])){//Se informar o ID do usuário que deseja modificar
    $senha = strip_tags(sha1(md5(trim($_POST['senha']))));//criptografa a senha
    $ID = $_POST ['ID'];//recebe o ID informado pelo usuario para modificar no sql
    if ((!empty($_POST['NEWID'])) || (!empty($_POST['usuario'])) || (!empty($_POST['senha']))){//Verifica se um dos campos foi passado
        $sql = "UPDATE usuarios SET";
        if (!empty($_POST['NEWID'])){//modifica o ID
            $sql .= " ID = :ID,";
        }
        if (!empty($_POST['usuario'])){//modifica o usuario
            $sql .= " usuario = :usuario,";
        }
        if (!empty($_POST['senha'])){//modifica a senha
            $sql .= " senha = :senha,";
        }
        substr($sql, 0, strlen($sql) - 1);
        $sql .= " WHERE ID = $ID";
        $stmtpass = $pdo->prepare($sql);
        if (!empty($_POST['NEWID'])) {
            $stmtpass->bindParam(':ID', $_POST['NEWID'], PDO::PARAM_STR);
        }
        if (!empty($_POST['usuario'])) {
            $stmtpass->bindParam(':usuario', $_POST['usuario'], PDO::PARAM_STR);
        }
        if (!empty($_POST['senha'])) {
            $stmtpass->bindParam(':senha', $_POST['senha'], PDO::PARAM_STR);
        }
        $stmtpass->execute();
    }
}

In addition to decreasing calls to server MySQL , it will also correct your problem of not changing usuario after changing ID .

    
23.10.2017 / 13:22