PHP: Will my code cause problems in the long run/is this bad practice?

I've recently finished the PHP course on codeacadamy and I'm currently building my first PHP site. This is my first time coding anything so please forgive the fact that my code might be very unorganized and crude. The site is for a scavenger hunt, and the idea is to have a dedicated page for each item of the hunt. I have two tables in my db that the pages access to check data. I've created a template page, which will basically be called in by each dedicated page. My code is all working as intended. I do, however, get a warning that the$game
and$question
variables have not been used. I understand why I am getting this warning, and that the solution will probably be to convert the majority of my code in scavenger_template.php into a function and do something likescavenger_function($game, $question);
on my individual pages.
My question is, if I leave the code the way it is, will this potentially cause problems in the long run? Is it better/good practice to functionize as much of the code as possible? If you have any tips for good practices or being efficient in coding, it would be greatly appreciated. Codeacademy doesn't really go into much detail about how to actually go about building something.
Thank you!
(game1.php)~(game00.php) dedicated pages
<?php
$game = 1;
$question = "What is 1+1?";
include 'scavenger_template.php';
?>
(scavenger_template.php)
<?php
session_start();
$username = $_SESSION["username"];
$empty_message = "";
$submission_message = "";
$submission_confirm = "";
$row2 = "";
include ('../db_connection.php');
include ('../addPoints.php');
$conn = OpenCon();
/* define game number for each page. game1 in users correlates to id=1 in games. */
/* declaring variables required to check if game has been submitted */
$sql = "SELECT game$game FROM `users` WHERE username = '$username'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$user_answer = trim(strtolower(($_POST["answer"])));
/*check to see if something is inputed */
if ($user_answer != "") {
/* check if submission is already made. if row = 0, then game hasn't been played */
if ($row[0] === "0") {
/* declaring variables required to check answers in games table */
$sql2 = "SELECT answer FROM `games` WHERE id = '$game'";
$result2 = mysqli_query($conn, $sql2);
$row2 = mysqli_fetch_array($result2);
/* check submitted answer against game table */
if ($user_answer === $row2[0]) {
$sql_value = "SELECT value FROM `games` WHERE id = '$game'";
$result_value = mysqli_query($conn, $sql_value);
$row3 = mysqli_fetch_array($result_value);
addPoints($row3[0], $username, $conn);
$sql3 = "UPDATE users SET game$game='1' WHERE username = '$username'";
mysqli_query($conn,$sql3);
$submission_confirm = "Your answer is correct!";
} else {
$submission_confirm = "Your answer is incorrect.";
$sql3 = "UPDATE users SET game$game='1' WHERE username = '$username'";
mysqli_query($conn,$sql3);
}
} else {
$submission_message = "Your team has already made a submission for this item.";
}
} else {
$empty_message = "Please input an answer.";
}
}
?>
<html>
<head>
</head>
<div align="center">
<form method="post">
<label><?= $question ?></label><br>
<input type="text" id="answer" name="answer"><br><br>
<input type="submit" name="submit" value="Submit Answer"><br>
</form>
<?= $submission_message?>
<?= $empty_message?>
<?= $submission_confirm?>
<br><br>
<form action="../home.php">
<input type="submit" value="Go to home" />
</form>
</div>
</html>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: closed without sending a request; it was probably just an unused speculative preconnection
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.