How to do multiple run with PDO

Asked

Viewed 113 times

-1

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

Follow the excerpt of the code I’m in 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 to modify the user also it will modify only the 1 item that in the case is the ID, my goal is to make it only be modified the items that the user type... I tried to do an execution only but the problem persisted..

  • But if you modify the ID and then try to modify the USER, the table ID will not be the ID you are comparing, but the ID that is coming by POST['NEWID'].

  • really, but in the case had already tried to do it at once with the execute and still persisted, if I performed everything together he should not do all? since the ID would only modify after the run, good anyway was really this! If you can answer the question to close the topic I would be grateful! really the most difficult is to see our mistakes..

1 answer

5


There’s nothing to do UPDATE, you can do only one. It will save running time 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 the server MySQL, will also fix your problem of not changing the usuario after changing ID.

  • what does it do? substr($sql, 0, strlen($sql) - 1); had already tried to do at once, I will review what was wrong, vlw.

  • substr serves to catch a part of a string, already the strlen account how many characters a string.

  • In that case, I’m calling this function to remove the last remaining comma in QUERY before the WHERE.

  • I saw this problem of the same comma, in case it is not working, I did it in a more crude way by putting AND and 7 IF operands (yes it is not a good practice).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.