获取数据MySQL DB并以HTML格式显示

时间:2017-02-06 21:07:45

标签: javascript php html css mysql

我如何使用PHP / JavaScript和HTML / CSS编写我的程序代码来显示我在MySQL Monitor中创建的数据库中的数据,如下所示:

enter image description here

我制作了使用PHP进入数据库并在HTML页面上显示数据的按钮:

HTML:

<form action="fullridez.php" method="post">
        <h4 id="Filter">GPA</h4>
        <input id="FilterBox" name="gpa" type="text"/>
        <h4 id="Filter">Amount</h4>
        <input id="FilterBox" name="amount" type="text"/>
        <h4 id="Filter">School</h4>
        <input id="FilterBox" name="school" type="text"/>
       <input  type="submit" id="FilterBox" name="myForm" onkeypress="checkEnter()" ><img src="search.png" width=15 height=15 /></button>
      </form>
      <script>
      </script>

PHP:

   <?php

if(isset($ _ POST [&#39; myForm&#39;])){

$servername = "localhost";
$username = "root";
$password = "";
$database = "scholarshiplist";

 $conn = mysqli_connect($servername, $username, $password, $database);

 $gpa = $_POST['gpa'];
 $amount = $_POST['amount'];


 $count = "SELECT * FROM scholarships";
 $result = mysqli_query($conn, $count);

 if ($result->num_rows > 0) {
    $sql = "SELECT * FROM scholarships WHERE GPA <= " . $gpa . " AND Amount <= " 
    . $amount;

     if ($result = mysqli_query($conn, $sql)) {
        while ($row=mysqli_fetch_row($result)) {
            for($i = 0; $i < count($row); $i++) {
                echo $row[$i] . '<br>';
            }
        } 
     }

 } else {
     echo "0 results";
}

$conn->close();

}

SQL:

USE ScholarshipList;

CREATE TABLE Scholarships
(
  id             int unsigned NOT NULL auto_increment, 
  School         varchar(500) NOT NULL,                
  GPA            decimal(10,2) NOT NULL,                
  Amount         decimal(10,2) NOT NULL,               

  PRIMARY KEY     (id)
);

我正在使用XAMPP

当我单击HTML文件上的按钮时,它会将我带到PHP页面,我看到的只是PHP代码。我不希望它转到该页面,但保持在显示按钮下方数据的同一页面上。

到目前为止这是页面的样子

page

enter image description here

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您可以使用for循环构建线框div表:

<?php 
$num_rows = mysql_num_rows($result);
for ($i=0;$i<$num_rows;$i++) { 
    //loop through all rows of data
    $row = mysql_fetch_assoc($result); // your data is now:  $row['fieldName']
    ?>
    <div>
        GPA <input name="" value="<?php echo($row['gpa'])?>;" type="text"> 
        AMOUNT <input name="" value="<?php echo($row['amount'])?>;"  type="text"> 
        SCHOOL <input name="" value="<?php echo($row['school'])?>;"  type="text">
    </div>
<?php 
} //end of the loop
?>

答案 1 :(得分:0)

如果你的HTML表单包含在'fullridez.php'文件中并且你将表单输入发布到同一个文件,那么你需要有一些PHP,你想要输出以检查结果,然后循环遍历这些结果,同时回应它们:

<table>
    <tr><td>Col 1</td><td>Col 2</td><td>Col 3</td></tr>
    <?php
        while($row = mysql_fetch_assoc($result))
        {
            echo "<tr><td>"
                . $row['col_1'] . "</td><td>"
                . $row['col_2'] . "</td><td>"
                . $row['col_3'] . "</td></tr>";
        }
    ?>
</table>