I am working on an assignment to have one main page with five pages where one database query is run on each. On the main page, I want to click on query 1, then query 1 page (q1.php) has a form and a submit button. When the submit button is pressed, the script runs, and the database selection is displayed underneath the button on the same page. I also want all the code on the same page.The way it is set up now is that as soon as you click and go to the query 1 page, the result is already displayed.
// connect to the database
$host = "xxxxxx";//set year for devweb
$user = "xxxxxx";//your username
$pass = "xxxxxx";//your MySQL password
$dbname = "xxxxxx";
$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error){
die("Connection failed : ".$conn->connect_error);
}
// isset to ensure query is either set or empty
$yesorno = isset($_POST["yesorno"]) ? $conn->real_escape_string($_POST["yesorno"]): "";
$sql = "SELECT Staff.DepotName, COUNT(S_id) from Staff, Depot WHERE Staff.DepotName=Depot.DepotName GROUP BY DepotName ORDER BY DepotName";
$result = $conn->query($sql);
Then this is the form action and code to display the database selection (q1.php is the name of the page the script is run on, referring to itself):
<form method="POST" action="q1.php">Type yes or no to run the query: <br>
<input type="text" name="yesorno"><br><br>
<input type="submit" name="submit" value="Submit">
</form></p></td></tr>
<?php
if ($result->num_rows > 0) {
echo "<tr><th>Name of depot</th><th>Number of staff</th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["DepotName"]."</td><td>".$row["COUNT(S_id)"]."</td></tr>";
}
echo "";
} else {
echo "0 results";
}
$conn->close();
How can I set this up so that the script runs and displays underneath the submit button once the submit button is pressed? It is there as the page loads, but I can't work out how to get the submit button to run it. I am pretty new at this, so any help would be much appreciated.