How to save a list from an Option

Asked

Viewed 39 times

0

I have the following code:

echo "<select id='posto[]' name='posto[]' multiple='multiple'>";
    foreach ($postos as $lista) {
        echo '<option value=' . $lista->getCodigoPosto() . '>' . $lista->getNomePosto() . '</option>';
    }
    echo "</td>";
    echo "</select>";

Inside a form with method GET, when the user does Submit I take the codes he selected in OPTION and put in a variable as if it were a list:

$postosSelecionados = $_REQUEST['posto'];

So I move this list into a recording method:

if (isset($_REQUEST['posto'])) {
        if (UsuarioCadastradoDAO::aprovar($codigo, $aprovador, $postosSelecionados) == FALSE) {
            echo "alert('Ocorreu algum erro ao aprovar, favor tentar novamente.');</script>";
        }
    }

See the method:

public static function aprovar($codUsuarioAprovado, $codUsuarioAprovador, $listaDePostos) {
        try {
            $banco = new Banco();
            $banco->setComandoSQL("INSERT INTO `UsuarioMasterAprovado` (`IdUsuario`, `IdUsuarioAprovador`, `DataAprovacao`) VALUES (:UsuarioCadastrado,:UsuarioAprovador, CURDATE()); UPDATE `Usuario` SET `Aprovado` = 1 WHERE IdUsuario = :UsuarioCadastrado");

            $parametros = array(
                ':UsuarioCadastrado' => $codUsuarioAprovado,
                ':UsuarioAprovador' => $codUsuarioAprovador,
            );

            $banco->ExecutaSQLPreparada($parametros);

            foreach ($listaDePostos as $lista) {

                $banco->setComandoSQL("INSERT INTO `Usuario_Posto`(`Posto_idPosto`, `UsuarioMasterAprovado_IdUsuario`) VALUES (:idPostos,LAST_INSERT_ID()");

                $parametros = array(
                    ':idPostos' => $lista
                );

                $banco->ExecutaSQLPreparada($parametros);
            }
            return TRUE;

        } catch (Exception $ex) {           
            return FALSE;
        }
    }

However something is wrong and I am not able to identify the error, because it is not recording in the database.

  • Is there an error? auto commit turned on? check if ExecutaSQLPreparada() returns some error.

  • The.o does not do return false; no catch at least shows the Exception, so you know at least where to start.

1 answer

2

One parenthesis is missing:

$banco->setComandoSQL("INSERT INTO Usuario_Posto(Posto_idPosto, UsuarioMasterAprovado_IdUsuario) VALUES (:idPosts,LAST_INSERT_ID()");

Correction:

$banco->setComandoSQL("INSERT INTO Usuario_Posto(Posto_idPosto, UsuarioMasterAprovado_IdUsuario) VALUES (:idPosts,LAST_INSERT_ID())");

  • Good, but it hasn’t worked yet...

Browser other questions tagged

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