Error trying to insert data into a database

0

I have the following functions:

function query($stmt){
    try{
        return mysqli_stmt_execute($stmt);
    }
    finally{
        mysqli_stmt_close($stmt);
        mysqli_close($GLOBALS['conexao']);
    }
}

function cadastrarNoBanco($nome, $email){
    $ip = getIp();
    $stmt = mysqli_prepare($GLOBALS['conexao'], "INSERT INTO tbl_lead VALUES(default, ?, ?, ?, default)");
    return query(mysqli_stmt_bind_param($stmt, 'sss', $nome, $email, $ip));
}

But you are giving the following error when calling cadastrarNoBanco() :

  

Warning: mysqli_stmt_execute () expects parameter 1 to be mysqli_stmt, boolean given

I've tried to pass stmt by reference, but it does not work. I thought it was an error in the query syntax, but if I run it directly in MySql, it looks good.

    
asked by anonymous 01.10.2017 / 15:47

1 answer

1

The mysqli_stmt_bind_param () function returns a boolean.

You should run it without assigning it to anyone, and then pass $ stmt to the query function. Ex:

function cadastrarNoBanco($nome, $email){
    $ip = getIp();
    $stmt = mysqli_prepare($GLOBALS['conexao'], "INSERT INTO tbl_lead VALUES(default, ?, ?, ?, default)");
    mysqli_stmt_bind_param($stmt, 'sss', $nome, $email, $ip)
    return query($stmt);
}

Tip: Take a look at the PDO and it is the most recommended option for working with a database when using an ORM. link

    
02.10.2017 / 00:21