0

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.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Rosita
  • 1
  • Don't run the query or display the data (apart from the form) unless the request is a HTTP POST (because just visiting the page initially would be via a GET request). See https://stackoverflow.com/questions/359047/detecting-request-type-in-php-get-post-put-or-delete – ADyson Apr 26 '22 at 09:20

1 Answers1

-1

if(!empty($_POST)) {

// `condition after form submit`

} else {

// on load

}

  • [How do I format my posts?](https://stackoverflow.com/help/formatting) – ADyson Apr 26 '22 at 09:26
  • Where do I put this code? Can you also please elaborate on what to put in the condition after form submit as well as on load? I don't understand. Thanks – Rosita Apr 26 '22 at 11:24
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '22 at 12:15