I wrote a function in PHP that is supposed to return the result of an SQL query into a variable, so i can use this variable later in the HTML part to show it on the webpage. Unfortunately, nothing shows up on the page, despite the SQL query being right. I don't know what's wrong or what's missing
here is my code
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
// Create connection
/* SHOW NOTE */
function show_note($ue){
$conn = new mysqli("-", "-", "-", "-");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
switch ($ue) {
case "1.1":
$sql = "SELECT ((SELECT (AVG(note.note)*21) FROM `note` WHERE user = ? AND matiere = 'devweb') + (SELECT (AVG(note.note)*42) FROM `note` WHERE user = ? AND matiere = 'initdev') + (SELECT (AVG(note.note)*6) FROM `note` WHERE user = ? AND matiere = 'econum')) / 60 FROM `note`";
$stmt = $conn->prepare($sql);
if ($stmt === false) {
printf("Message d'erreur : %s\n", $conn->error);
die();
}
$stmt->bind_param("sss", $_SESSION['username'],$_SESSION['username'],$_SESSION['username']);
$stmt->execute();
$result = $stmt->get_result();
$resultat = $result->fetch_row();
$conn->close();
break;
case "1.2":
break;
case "1.3":
break;
case "1.4":
break;
case "1.5":
break;
case "1.6":
break;
return $resultat[0];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body{ font: 14px sans-serif; text-align: center; }
.addnote{ width: 30%; margin: auto; border: solid 2px pink; }
.shownote{ width: 30%; margin: auto; border: solid 2px green; }
</style>
</head>
<body>
<h1 class="my-5">Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome to our site.</h1>
<p>
<a href="reset-password.php" class="btn btn-warning">Reset Your Password</a>
<a href="logout.php" class="btn btn-danger ml-3">Sign Out of Your Account</a>
</p>
<div class="addnote">
<h2 class="my-5">Ajouter une note</h2>
<form action="welcome.php" method="post">
Matière :
<input type="radio" id="anglais" name="matiere" value="anglais">
<label for="anglais">Anglais</label>
<input type="radio" id="mathdisc" name="matiere" value="mathdisc">
<label for="mathdisc">MathsDisc</label>
<input type="radio" id="mathfond" name="matiere" value="mathfond">
<label for="mathfond">MathsFond</label><br>
Coefficient : <input type="number" name="coeff"><br>
Note : <input type="number" name="note"><br>
<input type="submit" name="addnote">
</form>
</div>
<div class="shownote">
<h2 class="my-5">Vos moyennes</h2>
<p>UE 1.1 : <?php echo show_note("1.1") ?></p>
</div>
</body>
</html>
So basically, the return statement was inside the switch statement
Code before:
...
case "1.6":
break;
return $resultat[0];
}
}
Code after:
case "1.6":
break;
}
return $resultat[0];
}
Thanks to @akrys for seeing it and telling me that was the problem !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.