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:
Table: Products
Table: Categories
Relation Table: Products ~ Categories
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);
Possible duplicate of How to get the next ID to be inserted?
– Daniel Omine
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
– Daniel Omine