简体   繁体   English

在 PHP MYSQL 中获取一个按钮来显示查询结果

[英]Getting a Button to Display Query Results in PHP MYSQL

I am trying to run a query that will display in my browser when I click the button, but instead the results are just displayed from the beginning.我试图运行一个查询,当我单击按钮时,该查询将显示在我的浏览器中,但结果只是从头开始显示。 I have the button set to post and my php file as the action but it seems to just run the code from the beginning.我将按钮设置为发布,并将我的 php 文件设置为操作,但它似乎只是从一开始就运行代码。 Here is what I have:这是我所拥有的:

<?php
$host = "localhost";
$db = "cis475";
$user = "root";
$pw = "";


$conn = new mysqli ($host, $user, $pw, $db);
if($conn->connect_error) die($conn->connect_error);

$readAllQuery = "SELECT * FROM enrollment JOIN course ON enrollment.CourseID 
= course.CourseID JOIN student ON enrollment.StudentID = student.StudentID";

    $result = $conn->query($readAllQuery);
    if (!$result) die($conn->error);

    echo "<table border='1'>";
    echo "<tr><td>EnrollmentID</td><td>Grade</td><td>EnrollmentSemester</td> 
   <td>CourseID</td><td>StudentID</td><td>Title</td><td>Credits</td> 
   <td>LastName</td><td>FirstMidName</td></tr>";
    while ($row = mysqli_fetch_assoc($result)){
        echo "<tr><td>{$row['EnrollmentID']}</td><td>{$row['Grade']}</td> 
   <td>{$row['EnrollmentSemester']}</td><td>{$row['CourseID']}</td><td> 
   {$row['StudentID']}</td><td>{$row['Title']}</td><td>{$row['Credits']} 
   </td> 
   <td>{$row['LastName']}</td><td>{$row['FirstMidName']}</td></tr>";

    }
echo "</table>";

?>

<form method='post' action='readAll.php'>

<input type='submit' name='submit' value='Show All Enrollments'>

</form>

You need to see if 'something' is there (in this case, all you have is the submit) and then do the display....您需要查看是否存在“某物”(在这种情况下,您所拥有的只是提交),然后进行显示....

<?php
if($_POST['submit']){
    $host = "localhost";
    $db = "cis475";
    $user = "root";
    $pw = "";

    $conn = new mysqli ($host, $user, $pw, $db);
    if($conn->connect_error) die($conn->connect_error);

    $readAllQuery = "SELECT * FROM enrollment JOIN course ON enrollment.CourseID = course.CourseID JOIN student ON enrollment.StudentID = student.StudentID";

    $result = $conn->query($readAllQuery);
    if (!$result) die($conn->error);

    echo "<table border='1'>";
    echo "<tr><td>EnrollmentID</td><td>Grade</td><td>EnrollmentSemester</td> 
    <td>CourseID</td><td>StudentID</td><td>Title</td><td>Credits</td> 
    <td>LastName</td><td>FirstMidName</td></tr>";
    while ($row = mysqli_fetch_assoc($result)){
        echo "<tr><td>{$row['EnrollmentID']}</td><td>{$row['Grade']}</td> 
        <td>{$row['EnrollmentSemester']}</td><td>{$row['CourseID']}</td><td> 
        {$row['StudentID']}</td><td>{$row['Title']}</td><td>{$row['Credits']} 
        </td><td>{$row['LastName']}</td><td>{$row['FirstMidName']}</td></tr>";
    }
    echo "</table>";
}
?>

<form method='post' action='readAll.php'>

<input type='submit' name='submit' value='Show All Enrollments'>

</form>

Any code present in inside the program file will run upon execution, this is true for all languages, unless it is wrapped inside an if or any other conditional statements;程序文件中的任何代码都将在执行时运行,这对所有语言都是如此,除非它包含在if或任何其他条件语句中; in which case the code enclosed in the conditional statement will only be executed if the condition is true .在这种情况下,包含在条件语句中的代码只有在条件为true才会执行。

And thus to solve your issue, you'll need a conditional statement that checks if the form was submitted or not and if it was then execute the code that follows-因此,为了解决您的问题,您需要一个条件语句来检查表单是否已提交,是否已提交,然后执行以下代码-

if(isset($_POST['submit'])){
    $host = "localhost";
    $db = "cis475";
    $user = "root";
    $pw = "";

    $conn = new mysqli ($host, $user, $pw, $db);
    if($conn->connect_error) die($conn->connect_error);

    $readAllQuery = "SELECT * FROM enrollment JOIN course ON enrollment.CourseID = course.CourseID JOIN student ON enrollment.StudentID = student.StudentID";

    $result = $conn->query($readAllQuery);
    if (!$result) die($conn->error);

    echo "<table border='1'>";
    echo "<tr><td>EnrollmentID</td><td>Grade</td><td>EnrollmentSemester</td> 
    <td>CourseID</td><td>StudentID</td><td>Title</td><td>Credits</td> 
    <td>LastName</td><td>FirstMidName</td></tr>";
    while ($row = mysqli_fetch_assoc($result)){
        echo "<tr><td>{$row['EnrollmentID']}</td><td>{$row['Grade']}</td> 
        <td>{$row['EnrollmentSemester']}</td><td>{$row['CourseID']}</td><td> 
        {$row['StudentID']}</td><td>{$row['Title']}</td><td>{$row['Credits']} 
        </td><td>{$row['LastName']}</td><td>{$row['FirstMidName']}</td></tr>";
    }
    echo "</table>";
}

In this snippet, we use if statement to check if variable $_POST['submit'] exists.在这个片段中,我们使用if语句来检查变量$_POST['submit']存在。
Here submit with is the name of your form's submit button这里submit with 是表单提交按钮的名称
Note: a $_POST variable will only exist if the form was submitted, calling isset() will return false if the form wasn't submitted and as such your code inside { } will not be executed on first load but will run upon submit.注意: $_POST _ $_POST变量仅在表单已提交时才存在,如果表单未提交,则调用isset()将返回false ,因此您在{ }的代码不会在第一次加载时执行,但会在提交时运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM