ID error Category/Subcategory

Asked

Viewed 53 times

0

Banco da Categoria https://uploaddeimagens.com.br/images/000/857/341/original/3.png?1489170048

Bank of the Sub-category https://uploaddeimagens.com.br/images/000/857/342/full/4.png?1489170053

You see, when I create the categories and I include the subcategories in them, they’re simply adding in all the categories, I’ve gone over the code several times and I don’t understand. Ids remain at "0". What would be the path to error ?

<div id="configs">
<h1>Cadastrar/Excluir Categorias</h1>

<div class="cadastra">
    <form action="" method="post" enctype="multipart/form-data">
        <label>
            <span>Categoria</span>
            <input type="text" name="categoria" />
        </label>
        <input type="hidden" name="cadastrar" value="categoria" />
        <input type="submit" value="Cadastrar" />
    </form>
</div><!-- cadastra -->

<div class="exibe">
    <ul>
    <?php
        $listaCats = BD::conn()->prepare("SELECT * FROM `categorias` ORDER BY id DESC");
        $listaCats->execute();
        if($listaCats->rowCount() == 0){}else{
         while($categoria = $listaCats->fetchObject()){
    ?>
        <li><?php echo $categoria->nome;?> <a href="?pagina=categorias&excluir=sim&cat=<?php echo $categoria->id;?>"><img src="imgs/btn_del.png" border="0" width="17" /></a></li>
    <?php }}?>        
    </ul>
</div><!-- exibe -->
<?php if(isset($_POST['cadastrar']) && $_POST['cadastrar'] == 'categoria'){
    $categoria = strip_tags(filter_input(INPUT_POST, 'categoria'));

    if($categoria == ''){
        echo '<script>alert("Informe o nome da categoria!");location.href="?pagina=categorias"</script>';
    }else{
        $slugCat = $painel->slug($categoria);
        $cadastrar = BD::conn()->prepare("INSERT INTO `categorias` (nome, slug) VALUES (?,?)");
        if($cadastrar->execute(array($categoria, $slugCat))){
            echo '<script>alert("Categoria cadastrada com sucesso!");location.href="?pagina=categorias"</script>';
        }else{
            echo '<script>alert("Erro ao cadastrar categoria!");location.href="?pagina=categorias"</script>';
        }
    }
}


if(isset($_GET['excluir']) && $_GET['excluir'] == 'sim'){
    $cat = strip_tags((int)$_GET['cat']);
    $excluir = BD::conn()->prepare("DELETE categorias, subcat FROM categorias LEFT JOIN subcat ON(categorias.id = subcat.id_cat) WHERE categorias.id = ?");
    if($excluir->execute(array($cat))){
        echo '<script>alert("Categoria excluída com sucesso!");location.href="?pagina=categorias"</script>';
    }else{
        echo '<script>alert("Erro ao excluir categoria!");location.href="?pagina=categorias"</script>';
    }
}
?>

</div><!-- configs -->
  • The variables $categoria and $slugCat, bring the q?

  • Have you created in the table the id field as the primary key and auto-increment? If you didn’t create, delete and recreate the table by marking id as the primary key (PK - Primary key) and auto-increment (AI - auto-increment)

  • Buddy, I got it!! I was so furious that I followed his tips and it worked... Really on localhost (my server), I was right about the attributes of the ID, but the hosting was not auto increment, I deleted and remade and it worked... Thanks!

No answers

Browser other questions tagged

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