0
I created a discussion topics registration form. Discussion topics are linked to a category via a foreign key. Then, 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 I register in the form. They are not entered 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:
Does not identify the ID, which is the id of the category Linkei to register a topic.
The ID is not treated by the form. It is the primary key of the category, as in the figure below:
It appears in the url:
Is there a mistake? You’re getting into that
if
?– Lucas
Checks if the ID is coming by GET or POST. The second error says it is calling a function from a variable that is not an object. Probably the
prepare
is returningfalse
– Lucas
The ID is not being passed. Place it in the form action (GET) or place it inside the form in a Hidden field (POST)
– Lucas
Putting in the form action worked. Using an Hidden field conflicted with fetch_assoc()
– Luana Santos