How do I get the autoincrement id without being registered?

Asked

Viewed 167 times

1

Hello,

How do I get the id that is autoincrement without the item being registered in the database?
Or I’ll have to do 2 process, register the post first, then register the post in the category ?

I have a system to register posts here, and the tables are separate so I can register the same post in several categories: inserir a descrição da imagem aqui

Table: Products

inserir a descrição da imagem aqui

Table: Categories

inserir a descrição da imagem aqui

Relation Table: Products ~ Categories

Tabela de relação entre produtos e categorias

PHP code to register the post:

$cadastrarItem = $conexao->prepare("INSERT INTO tb_mark (name_mark, description_mark, keywords, att_mark, image_mark, link) VALUES (:title, :description, :keywords, NOW(), :img, :linkitem)");
                $cadastrarItem->bindParam(':title', $title, PDO::PARAM_STR);
                $cadastrarItem->bindParam(':img', $novoNome, PDO::PARAM_STR);
                $cadastrarItem->bindParam(':description', $description, PDO::PARAM_STR);
                $cadastrarItem->bindParam(':keywords', $tags, PDO::PARAM_STR);
                $cadastrarItem->bindParam(':linkitem', $linkitem, PDO::PARAM_STR);

                $verificaItens = $conexao->prepare("SELECT name_mark FROM tb_mark WHERE name_mark=:title");
                $verificaItens->bindParam(':title', $title, PDO::PARAM_STR);
                $verificaItens->execute();

                if($verificaItens->rowCount() == 0)
                {
                    $cadastrarItem->execute();
                    echo '<script language= "javascript">
                    location.href="/admin_include_brand/register_item";
                    </script>';
                }
                else
                {
                    echo '<script language= "javascript">
                    location.href="/admin_include_brand/register_error";
                    </script>';
                }

And that would basically be it for me to enter the category in the table:

    $cadastrarItemCategoria = $conexao->prepare("INSERT INTO tb_category_itens (id_item, id_category) VALUES (:id_item, :id_category)");
// id_item = 'id' da tabela produtos
// id_category = 'id' da tabela categorias
    $cadastrarItemCategoria->bindParam(':id_item', $id_item, PDO::PARAM_STR);
    $cadastrarItemCategoria->bindParam(':id_category', $category, PDO::PARAM_STR);
  • 2
  • If you still want to continue, here is a related topic, but please read the link above before this: http://answall.com/questions/99347/comoria-c%C3%B3digo-antes-de-insert-dados-no-banco/99348#99348

2 answers

5


Boy I’ve had this trouble these days

SHOW TABLE STATUS LIKE 'nomedatabela' 

here as I used

try {
$sql = "SHOW TABLE STATUS LIKE 'nomedatabela' ";  
$stmt = $DB->prepare($sql);
$stmt->execute();
$resultado = $stmt->fetch();
$proximoID = $resultado['Auto_increment'];  // a chave esta aqui
 } catch (Exception $ex) {
 echo $ex->getMessage();
}
echo $proximoID;

0

There is a method using the information_schema.

The construction of Mysql will be:

SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = <NOME DA TABELA> AND table_schema = <NOME DO BANCO DE DADOS>

For example:

<?php

// Dados necessários:
$nome_tabela = 'tabela';
$nome_banco = '';

// Se deixar o $nome_banco em branco isto irá usar a função DATABASE():
$nome_banco = ($nome_banco != '') ? "'".$nome_banco."'" : "DATABASE()";

// MYSQLI! Irá efetuar a query:
$id = $mysqli->query("SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = '$nome_tabela' AND table_schema = $nome_banco");

// Irá pegar e exibir o AUTO_INCREMENT:
$id = $id->fetch_array();
$id = $id['AUTO_INCREMENT'];
echo $id;

?>

Browser other questions tagged

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