0
I’m beginner in php, I’m doing a quiz as a college job...
By clicking submit, using a echo
in the variable $_SESSION['id']
, normally displays the content (receives the last id inserted in the database). However, when <form action="resultado.php" method="post">
and load the page is returned.
Notice: Undefined index: id in C: xampp htdocs inteligencias result.php on line 21
I need to recover this id on another page to perform sql query with the result.
questoes.php :
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Questionário</title>
</head>
<body>
<?php
include("conecta.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$st = $db->prepare("INSERT INTO usuarios (dt_resp) VALUES (NOW())");
$st->execute();
$_SESSION['id'] = $db->lastInsertId();
for ($i = 1; $i <= 28; ++$i) {
$chave = 'P'.$i;
$stmt = $db->prepare("INSERT INTO respostas (RESPOSTA, IDPERGUNTA, IDUSUARIO) VALUES (:resp, :perg, :id)");
$stmt->bindParam(":resp", $_POST[$chave]);
$stmt->bindParam(":perg", $i);
$stmt->bindParam(":id", $ident);
$stmt->execute();
}
}
?>
<form action="resultado.php" method="post"> <!-- resultado.php -->
<legend id="titulo">Inteligências Múltiplas</legend>
<?php
try {
foreach ($db->query("SELECT * FROM perguntas") as $perguntas) {
echo "<h4> $perguntas[ID]. $perguntas[ENUNCIADO] </h4>";
echo "<input type ='radio' id='concordo' name='P$perguntas[ID]' value = 'C' >Concordo<br>";
echo "<input type ='radio' id='discordo' name='P$perguntas[ID]' value = 'D' checked=checked >Discordo <br>";
}
$db = null;
} catch (PDOException $e) {
echo "Erro!: ".$e->getMessage()."</br>";
}
?>
<br>
<input type="submit" name="acao" value="ENVIAR">
</form>
</body>
</html>
php result.:
<?php
if (!isset($_SESSION)) {
session_start();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Resultado</title>
</head>
<body>
<?php
include("conecta.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo $_SESSION['id'];
}
?>
</body>
</html>
You have already run this test: <?php session_start(); ?>?
– Taffarel Xavier
Place the attribute
name
of radio Buttons asname='id'
.– Augusto Vasques
@Taffarelxavier
– Vitor Lopes
@Augustovasques need to capture the id of the question coming from the database to enter the answer, so theoretically I can’t change it. $_SESSION['id'] refers to the last id inserted in the user table.
– Vitor Lopes
But the question raised in the question is the fact that you do not have the 'id' parameter when you send the request to php result. that’s why it makes the mistake
Undefined index
. In the<form>
add an Hidden field whosename='id'
andvalue=$_SESSION['id']
– Augusto Vasques