mysql中获取显示查询结果的按钮

xcitsw88  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(442)

我正在尝试运行一个查询,当我单击按钮时,它将显示在浏览器中,但是结果只是从头开始显示。我将按钮设置为post,并将php文件设置为action,但它似乎只是从一开始就运行代码。以下是我所拥有的:

<?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>
3j86kqsm

3j86kqsm1#

程序文件中的任何代码都将在执行时运行,这对所有语言都适用,除非它 Package 在 if 或任何其他条件陈述;在这种情况下,条件语句中包含的代码只有在条件为 true .
因此,为了解决您的问题,您需要一个条件语句来检查表单是否已提交,然后执行下面的代码-

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>";
}

在这个片段中,我们使用 if 语句来检查变量 $_POST['submit'] 存在。
在这里 submit with是表单的提交按钮的名称
注:a $_POST 变量只在表单提交时存在,调用 isset() 会回来的 false 如果表单没有提交,因此你的代码在里面 { } 不会在第一次加载时执行,但会在提交时运行。

t3psigkw

t3psigkw2#

您需要查看是否存在“something”(在本例中,您所拥有的只是提交),然后执行显示。。。。

<?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>

相关问题