Capture value of last INSERT

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 news link 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 it inserts into the database, and right after it should UPDATE , it until it performs UPDATE but does not have the news ID, just the URL

    
asked by anonymous 02.05.2016 / 20:29

1 answer

3

Use the mysql LAST_INSERT_ID () function to return the last inserted id. Put this snippet right after your insert after the $ error variable

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

    
02.05.2016 / 20:48