Dlaczego poniższy kod nie dostaje się do instrukcji echo "I'm here 1"; oraz echo "I'm here 2";? Z logicznego punktu widzenia powinno być to nie możliwe. Przecież jest if z GET, który się nie wykona, więc powinna użyć się klauzula else, natomiast u mnie nic się nie wypisuje.
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Question</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>
<a href="index.php">
<img id="logo" src="logo.png" width="100px" height="100px" style="margin-left: 400px;">
</a>
</div>
<?php
$connection = mysqli_connect("XXX", "XXX", "XXX", "XXX");
if (isset($_POST['answer'])) {
$stmt = mysqli_prepare($connection, "INSERT INTO `answers`(`questionId`, `dateAndTime`, `answer`) VALUES (?,(SELECT now()),?");
$stmt->bind_param("ss", $a, $b);
$a = $_SESSION['id'];
$b = $_POST['answer'];
$stmt->execute();
$result = $stmt->get_result();
if (!$result) {
echo mysqli_error($connection);
}
else{
echo "Done";
}
}
if (isset($_GET['id'])) {
echo "I'm here 1";
$stmt = mysqli_prepare($connection, "SELECT posts.title,posts.body FROM posts WHERE posts.id=?");
$stmt->bind_param("s", $id);
$id = $_GET['id'];
$_SESSION['id'] = $id;
$stmt->execute();
$result = $stmt->get_result();
while ($row = mysqli_fetch_assoc($result)) {
echo '<div style="font-size: 42px;">', $row['title'], "</div><div>", $row['body'], "</div>";
}
} else {
echo "I'm here 2";
$stmt = mysqli_prepare($connection, "SELECT posts.title,posts.body FROM posts WHERE posts.id=?");
$stmt->bind_param("s", $id);
$id = $_SESSION['id'];
$stmt->execute();
$result = $stmt->get_result();
while ($row = mysqli_fetch_assoc($result)) {
echo '<div style="font-size: 42px;">', $row['title'], "</div><div>", $row['body'], "</div>";
}
}
?>
<form action="question.php" method="POST" style="display: flex; flex-direction: column;">
<textarea name="answer" style="margin-top: 100px; width: 25%; height: 200px;"></textarea>
<button type="submit" class="btn" style=" width: 5%; text-align: center;">Answer</button>
</form>
</body>
</html>