The form does not send data to the bank

0

I've created a discussion topic registration form. Discussion topics are linked to a foreign key category. So if I click on a category a list of topics appears. What I want is to be able to register a topic linked to a category with a form on the same page.

The problem occurs when I submit the data that I register on the form. They are not inserted in the database.

Below is the code:

<?php

include_once('conexao/conexao.php');


    if(($_SERVER["REQUEST_METHOD"] == "POST") and ($_POST["inserir"])){


        $titulo = $_POST['titulo'];
        $conteudo = $_POST['conteudo'];
        $data_criacao = date("Y/m/d h:i:s");
        $filtro_topico = $_GET['ID'];

        $stmt = $conecta->prepare("INSERT INTO discussao (titulo, conteudo, id_situacao, id_topico, id_usuario) VALUES ('$titulo', '$conteudo',  1, '$filtro_topico', 3)");
        $stmt->execute();
        header('location: index.php');
}
 ?>
<!DOCTYPE html>
<html>
   <head>
    <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
            <link rel="stylesheet" href="css/style.css">
            <script src="js/script.js"></script>
</head>
<body>
     <?php
                include('templates/header.html.php');
        ?>
                <?php

                ?>

                    <div class="btn" style="margin-top: 150px; margin-left: 1020px;">
                        <a>Novo Tópico</a>
                    </div>

                        <form class="form_discussion" action="<?php echo($_SERVER["PHP_SELF"]);?>" method="post" style="margin-top: 0px; margin-left: 260px;">
                            <h3>Criar Tópico de Discussão</h3>
                            <label>Título:</label>
                                <input type="text" name="titulo"><br><br>
                            <label>Conteúdo:</label><br>
                                <textarea name="conteudo"></textarea><br>
                            <input type="submit" value="Criar" name="inserir">
                            <input type="submit" value="Visualizar">
                            <input type="submit" value="Limpar">
                        </form>

                    <?php
                        $filtro_pagina = $_GET["ID"];
                        $query_topico = "SELECT titulo FROM topico WHERE id = " .$filtro_pagina;
                        $rs_topico = $conecta->query($query_topico);
                        while($lista_topico = $rs_topico->fetch_array()){

                    ?>

                    <div class="head_subtopics" style="margin-top: 30px; margin-left: 260px;">
                        <h2>Tópicos de <?php echo $lista_topico['titulo']; ?></h2>
                    </div>
                    <table class="t_subtopics" style="margin-top: 5px; margin-left: 260px;">

                    <?php
                        }
                        $filtro_topico = $lista_topico['id'];
                        $query_discussao = "SELECT DISTINCT discussao.id AS id, discussao.titulo AS titulo FROM discussao, topico WHERE discussao.id_topico = " .$filtro_pagina;
                        $rs_discussao = $conecta->query($query_discussao) or trigger_error($conecta->error."[$query_discussao]");
                        while($lista_discussao = $rs_discussao->fetch_assoc()){
                    ?>
                            <tr>
                                <td style="width: 500px;">
                                    <h3><a href="discussao-respostas.php?ID=<?=$lista_discussao['id'];?>"><?php echo $lista_discussao['titulo']; ?></a></h3>
                                </td>
                                <td style="width: 200px;">
                                    <ul>
                                        <li>34 respostas</li>
                                    </ul>
                                </td>
                                <td style="width: 200px;">
                                    <ul>
                                        <li>PDO MVC</li>
                                        <li>12:34</li>
                                        <li>por Web-user</li>

                                    </ul>
                                </td>
                            </tr>

                    <?php


                        }
                    ?>

                    </table>

            <?php
                include('templates/footer.html.php');
         ?>
</body>

The errors are shown in the figure below:

DoesnotidentifytheID,whichistheidofthecategorythatIlinkeitoregisteratopic.

TheIDisnothandledbytheform.Itistheprimarykeyofthecategory,likethefigurebelow:

It appears in the url:

link

    
asked by anonymous 26.05.2015 / 21:50

1 answer

1

The error is very descriptive, the variable $ _GET ['ID'] is not defined.

Solution:

if(isset($_GET['ID']))
    $filtro_topico = $_GET['ID'];
else 
    $filtro_topico = ID_PADRAO;//ID_PADRAO =  valor padrao

In addition, the prepare function is not returning an object, check if the error is caused by the problem described above, if it is not, check inside your database connection file.

    
26.05.2015 / 22:09