Capture last INSERT value

Asked

Viewed 82 times

1

I am creating a news system where the user has the option to register the news and put the original link where the news was generated, so far so good, the problem is that I want the user to have the option to register the news with the very link of the news that will be generated, I’m doing it this way:

        $urlNoticia = "index.php?".PARAMETER_NAME_ACTION."=show&";
        $urlNoticia .= PARAMETER_NAME_FILE."=exibir_noticia";
        $urlNoticia .= "&noticia_id=".$model->getnoticia_id();

        if(isset($_POST['FLinkNoticia'])) {
            $error = $this->getConexao()->executeNonQuery($sql);            
            $sqll  = "UPDATE noticia SET not_link = '".URL.$urlNoticia."SELECT MAX(noticia_id) FROM noticia' ";
            $sqll .= "WHERE not_titulo = '".$model->getnot_titulo()."'";
            $error = $this->getConexao()->executeNonQuery($sqll);
        } else {
            $error = $this->getConexao()->executeNonQuery($sql);
        }

Flinknoticia is a checkbox.

At first $error he makes the Insert in the bank, and shortly after he must make a UPDATE, it until it realizes the UPDATE but does not bring the news ID, only the URL

  • 1

    you want to catch the id table in update?

  • Yeah, I want to take the last id inserted ! @rray

  • I don’t understand your question. Don’t you already have the id value before you update? By the way why don’t you use the news id in Where?

1 answer

3


Use the LAST_INSERT_ID() function of mysql that will return the last inserted id. Place this snippet just after your Sert after the $error variable

$ultimoId = $this->getConexao()->executeNonQuery('LAST_INSERT_ID()');

  • How the DBMS will know which is the last ID if the field name is different ?

  • I don’t understand @Ikarosales, regardless of the field name the server returns the value of the field that is primary key in the table, this information is available in the table or DML metadata

Browser other questions tagged

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