Function mysql_insert_id() returns only zero

Asked

Viewed 537 times

1

I am having problems with the function mysql_insert_id(), when I use the form below the value returned is always "0" (zero). Can help me?

The "add" function calls the "save" function".

OPEN_DATABASE FUNCTION

// ABRE A BASE DE DADOS
function open_database()
{
    try
        {
            $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
            return $conn;
        }
    catch (Exception $e)
        {
            echo $e->getMessage();
            return null;
        }
}

ADD FUNCTION

// INSERE NOVO ORÇAMENTO
function add()
{
    if (!empty($_POST['budget']))
        {
            $budget = $_POST['budget'];
            save('budgets', $budget);
            //print_r($budget);
            //header('location: view.php?id='.$id);
        }
}

SAVE FUNCTION

// INSERE UM NOVO REGISTRO NO BANCO DE DADOS
function save($table = null, $data = null)
{
    $database = open_database();
    $columns = null;
    $values = null;
    foreach ($data as $key => $value)
        {
            $columns .= trim($key, "'") . ", ";
            $values .= "'$value', ";
        }
    // remover a ultima vÍrgula
    $columns = rtrim($columns, ', ');
    $values = rtrim($values, ', ');
    $sql = "INSERT INTO " . $table . " ($columns) " . "VALUES " . "($values);";
    //echo $sql;
    try
        {
            $database -> query($sql);
            $id = mysql_insert_id();
            header('location: view.php?id='.$id);
            $_SESSION['message'] = 'Registro cadastrado com sucesso!';
            $_SESSION['type'] = 'success';
        }
    catch (Exception $e)
        {
            $_SESSION['message'] = 'Nao foi possivel realizar a operacao!';
            $_SESSION['type'] = 'danger';
        }
    close_database($database);
}
  • Question: The record is saved correctly in the database?

  • The old functions mysql_ do not launch Exception, the try-catch is just for show.

  • 1

    You’re probably using another driver to connect to the bank, which is good, since mysql_ is obsolete and has been discontinued. The answer depends on how your function is open_database, include its content in the question.

  • @bfavaretto is up to date...

  • @Andersoncarloswoss yes, is saved normally...

  • 2

    Lacked a i in the call and pass the connection to function.

  • 2

    That is to say, mysqli_insert_id($database).

Show 2 more comments

1 answer

3


You’re using mysqli and on the line $id = mysql_insert_id(); mysql

Missed also pass connection $database for the function $id = mysqli_insert_id($database);

    ..........
    ..........
    try
    {
        $database -> query($sql);
        $id = mysqli_insert_id($database);
        header('location: view.php?id='.$id);
        $_SESSION['message'] = 'Registro cadastrado com sucesso!';
        $_SESSION['type'] = 'success';

    ..........
    ..........

Browser other questions tagged

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