php function does not insert records in the database

Asked

Viewed 421 times

2

I have a php function that inserts two types of record in a Mysql table, according to the user’s choice in a combobox. The first combo option enters the records normally, but when I choose the second one it does not insert and does not show any error. In the Inspect element I see that all the form parameters are being passed correctly. Can someone help me ? Thank you

Follows the code:

if($funcao == 'email') {

$sql = "select * from mensageminterna where mensagem like '%<a h%';";
$rst = my_query($connR, $sql);

    if($option == 'corretor'){

        $sql = "insert into mensageminterna (codempresa, codusuarioremetente, codusuario, mensagem, icone, datacriacao)
                select u.codempresa, u1.codusuario, u.codusuario, '$texto', 'cool', now()  from usuario u
                inner join usuario u1 on u.codempresa=u1.codempresa and u1.email like 'admin@%' and u1.indadministrador=1
                where u.codsituacaousuario=1;";
        $rst = my_execute($connW, $sql);

} elseif($option == 'gestor'){

        $sql = "insert into mensageminterna (codempresa, codusuarioremetente, codusuario, mensagem, icone, datacriacao)
            select u.codempresa, u1.nome remetente, u.nome destinatario, u.codtipousuario, '$texto', 'cool', now()  from usuario u
            inner join usuario u1 on u.codempresa=u1.codempresa and u1.email like 'admin@%' and u1.indadministrador=1
            inner join tipousuario tu on tu.codtipousuario = u.codtipousuario
            where u.codsituacaousuario=1 and tu.nome like '%geren%' or tu.nome like '%diret%' or tu.nome like '%coord%' or tu.nome like '%adm%'  or tu.nome like '%super%';";
        $rst = my_execute($connW, $sql);

} else {
        echo "Não enviado";
    }

exit;
}

<script language="JavaScript">
    $(document).on('click', '#btnEnviar', function(event) {
        event.preventDefault();
        $("#funcao").val("email");
        var self = $(this);
        $.ajax({
            url: "/email-broadcast.php",
            type: "POST",
            timeout:default_timeout,
            data: $('#formemail').serialize(),
            beforeSend: function(){
                self.attr('disabled', 'true');
            },
            success: function() {
                alert("Enviado com sucesso !");
            },
            error: function(jqXHR, textStatus){
                console.log(textStatus, jqXHR);
            },
            complete: function(){
                self.removeAttr('disabled');
            }
        });
    });

</script>


<form method="post" name="formemail" action="/email-broadcast.php" id="formemail">
        <input type="hidden" name="funcao" id="funcao" value="email"/>


            <label style="margin-left: 7px">Destinatário:</label>
            <select class="form-control" name="destinatario" id="destinatario" style="width: 250px;">
                <option value="corretor" id="corretor" name="corretor">Corretores</option>
                <option value="gestor" id="gestor" name="gestor">Gestores</option>
            </select>



        <textarea cols="86" rows="15" id="scriptenvio" name="scriptenvio" style=" margin-top: 10px;">
            Blá blá blá
        </textarea><br>


    <button type="button" class="btn btn-default" id="btnEnviar" name="btnEnviar">Enviar</button>
    </form>

select 2ª query

I took the select of the second query (the one I wasn’t inserting) and it returned 464 records.

  • What API are you using to connect to the database? could you show where the attribution of $option?

  • 1

    I use the mysqli...

  • And the assignment of $option where is?

  • global $option; $option = postget('destinatario'); stands before the start of the function

  • See if the bank returned any error, with mysql_error($conexao); make an if on the call from mysqli_query() or mysqli_execute()

  • So... the my_excecute function already handles the exceptions: try { $before = microtime(true); if (! mysqli_query ( $conn, $query )) { if (IS_DEV) { echo "<hr>DB ERROR: " . mysqli_error ( $conn ) . "<hr>"; } addLog ( 'db.log', $query . '|'.$paginaatual.'|'. mysqli_error ( $Conn ) );&#xA; throw new Exception(mysqli_error ( $Conn ));&#xA; #Exit();` }

  • So there are no records that satisfy the select conditions. You can try to copy the query and test directly in the database with simulated values (the same sent in the form) and see if it returns something.

  • I don’t have access to xD chat

  • I’ll print it out in response

  • Remove that exit; of the code, it seems lost ...

  • Your job my_execute() returns error when failed ? The message "unwrapped" has only effect if the variable $opcao is not equal to gestor or corretor.

  • No error returns, even I receive the message alert sent and all parameters are passed in the data form

Show 8 more comments

1 answer

1

Solved!

Actually, I was trying to insert varchar data into int fields in the second query. This was the correct one:

} elseif($option == 'gestor'){

    $sql = "insert into mensageminterna (codempresa, codusuarioremetente, codusuario, mensagem, icone, datacriacao)
        select u.codempresa, u1.codusuario, u.codusuario, '$texto', 'cool', now()  from usuario u
        inner join usuario u1 on u.codempresa=u1.codempresa and u1.email like 'admin@%' and u1.indadministrador=1
        inner join tipousuario tu on tu.codtipousuario = u.codtipousuario
        where u.codsituacaousuario=1 and tu.nome like '%geren%' or tu.nome like '%diret%' or tu.nome like '%coord%' or tu.nome like '%adm%'  or tu.nome like '%super%';";
    $rst = my_execute($connW, $sql);

}

Thank you all so much for your help !

  • So your function does not treat the exceptions, because you spoke no error was displayed.

Browser other questions tagged

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