Error when trying to insert data into a database

Asked

Viewed 34 times

0

I have the following duties:

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’re making the following mistake by calling cadastrarNoBanco():

Warning: mysqli_stmt_execute() expects Parameter 1 to be mysqli_stmt, Boolean Given

I tried to pass the stmt by reference, but it doesn’t work. I thought it was an error in the query syntax, but if I run it directly in Mysql, it is good.

  • Do not pass mysqli_stmt_bind_param as parameter. It returns a boolean, not stmt. In fact this function receives stmt by reference and modifies it, so call this function separately and pass the direct stmt as parameter of query.

  • @Andersoncarloswoss It worked! Want to make an answer to that?

1 answer

1


The function mysqli_stmt_bind_param() returns a Boolean.

You must execute 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 it is the most recommended option to work with database when using a ORM. http://php.net/manual/en/book.pdo.php

  • http://php.net/manual/en/mysqli-stmt.bind-param.php

Browser other questions tagged

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