基于html表单输入并显示在html页面中显示从数据库中提取的数据 [英] Show data pulled from database, based on html form input and display in html page

查看:220
本文介绍了基于html表单输入并显示在html页面中显示从数据库中提取的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有输入框的html页面。我试图从数据库中检索基于该输入的数据,并使用php以表格的形式将其发送回原始html页面。我在一个外部文件中创建了php,它在浏览器中显示了正确的值,但我的问题是试图在html页面上显示php。

我让我的服务器解析html文件,并将php放在html文件中,但仍然无法正常工作。当我输入一个值并提交页面重新加载,但没有显示。



如果有人知道这样做的最佳方式,我会很感激建议。我已经阅读了许多线索,似乎没有任何工作。

PHP:

 <?php 

define('DB_NAME','数据库');
define('DB_USER','user');
define('DB_PASSWORD','password');
define('DB_HOST','localhost');

$ conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
if(!$ conn){
die('Could not connect:'。mysqli_connect_error());
}

$ studentnum = $ _POST ['studentnum'];

$ sql =SELECT * FROM test WHERE number LIKE'%$ studentnum%';
$ result = mysqli_query($ conn,$ sql);

echo'< table class =table table-striped table-bordered table-hover>';
echo< tr>
< th>名称< th>
< th> Number< / th>
< th> Floor< / th>
< th>
< th> Message< th>
< / tr>;
while($ row = mysqli_fetch_array($ result,MYSQLI_ASSOC))
{
echo< tr>< td>;
echo $ row ['name'];
echo< / td>< td>;
echo $ row ['number'];
echo< / td>< td>;
echo $ row ['floor'];
echo< / td>< td>;
echo $ row ['room'];
echo< / td>< td>;
echo $ row ['message'];
回显< / td>< / tr>;

}
echo< / table>;

mysqli_close($ conn);
?>

HTML:

 < form action =test.phpmethod =post> 
< label>输入学号:< / label>
< input name =studentnumtype =numberplaceholder =输入此处>
< br>
< br>
< input type =submitvalue =Enter>

< / form>


解决方案

如果您想将它全部放在同一页面上,使用下面的代码,假设你在html文件中解析php正在工作,并且你的页面是index.html(如表单操作中指定的那样)。

 <?php 

//检查表单是否已提交并显示结果
if(isset($ _ POST ['studentnum'])){

define('DB_NAME','database');
define('DB_USER','user');
define('DB_PASSWORD','password');
define('DB_HOST','localhost');

$ conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
if(!$ conn){
die('Could not connect:'。mysqli_connect_error());
}

//为了防止注入攻击,转义后数据
$ studentnum = mysqli_real_escape_string($ conn,$ _POST ['studentnum']);

$ sql =SELECT * FROM`test` WHERE`number` LIKE'%$ studentnum%'';
$ result = mysqli_query($ conn,$ sql);

//检查查询是否返回结果
if(!$ result){
echo'没有搜索结果';
} else {
//结果输出表
echo'< table class =table table-striped table-bordered table-hover>';
echo< tr>
< th>名称< th>
< th> Number< / th>
< th> Floor< / th>
< th>
< th> Message< th>
< / tr>;
while($ row = mysqli_fetch_array($ result,MYSQLI_ASSOC))
{
echo< tr>< td>;
echo $ row ['name'];
echo< / td>< td>;
echo $ row ['number'];
echo< / td>< td>;
echo $ row ['floor'];
echo< / td>< td>;
echo $ row ['room'];
echo< / td>< td>;
echo $ row ['message'];
回显< / td>< / tr>;
}
echo< / table>;
}

mysqli_close($ conn);
} //结束提交
else
{
//未提交输出表单
?>
< form action =index.htmlmethod =post>
< label>输入学号:< / label>
< input name =studentnumtype =numberplaceholder =输入此处>
< br>
< br>
< input type =submitvalue =Enter>
< / form>
<?php
} // end未提交
?>


I have a html page with an input box. I am trying to retrieve data from the database based on that input and send it back to the original html page in the form of a table using php. I created the php in an external file and it was displaying the correct values in the browser but my issue is trying to display the php on the html page.

I enabled my server to parse html files and have put the php within the html file but still was not working. When I entered a value and submit the page reloads but nothing is displayed.

If anyone knows the best way of doing this I would appreciate the advice. I have read numerous threads and none seem to work.

PHP:

<?php

define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
     die('Could not connect: ' . mysqli_connect_error());
     }

$studentnum = $_POST['studentnum'];     

$sql = "SELECT * FROM test WHERE number LIKE '%$studentnum%'"; 
$result=mysqli_query($conn, $sql);

echo '<table class="table table-striped table-bordered table-hover">'; 
echo "<tr>
      <th>Name</th>
      <th>Number</th>
      <th>Floor</th>
      <th>Room</th>
      <th>Message</th>
      </tr>"; 
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
  echo "<tr><td>"; 
  echo $row['name'];
  echo "</td><td>"; 
  echo $row['number'];
  echo "</td><td>";   
  echo $row['floor'];
  echo "</td><td>"; 
  echo $row['room'];
  echo "</td><td>";   
  echo $row['message'];
  echo "</td></tr>";  

}
echo "</table>";  

mysqli_close($conn);
?>

HTML:

<form action="test.php" method="post">
            <label>Enter Student Number:</label>
            <input name="studentnum" type="number" placeholder="Type Here">
                <br>
                <br>
            <input type="submit" value="Enter">

    </form>

解决方案

If you want it all on the same page you can use the below, assuming your parse php in html files is working and your page is index.html (as specified in the form action).

<?php

// check if the form has been submitted and display the results
if (isset($_POST['studentnum'])) {

  define('DB_NAME', 'database');
  define('DB_USER', 'user');
  define('DB_PASSWORD', 'password');
  define('DB_HOST', 'localhost');

  $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$conn) {
    die('Could not connect: ' . mysqli_connect_error());
  }

  // escape the post data to prevent injection attacks
  $studentnum = mysqli_real_escape_string($conn, $_POST['studentnum']);

  $sql = "SELECT * FROM `test` WHERE `number` LIKE '%$studentnum%'"; 
  $result=mysqli_query($conn, $sql);

  // check if the query returned a result
  if (!$result) {
      echo 'There are no results for your search';
  } else {
    // result to output the table
    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr>
          <th>Name</th>
          <th>Number</th>
          <th>Floor</th>
          <th>Room</th>
          <th>Message</th>
          </tr>"; 
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
      echo "<tr><td>"; 
      echo $row['name'];
      echo "</td><td>"; 
      echo $row['number'];
      echo "</td><td>";   
      echo $row['floor'];
      echo "</td><td>"; 
      echo $row['room'];
      echo "</td><td>";   
      echo $row['message'];
      echo "</td></tr>";  
    }
    echo "</table>";
  }

  mysqli_close($conn);
} // end submitted
else
{
// not submitted to output the form
?>
<form action="index.html" method="post">
  <label>Enter Student Number:</label>
  <input name="studentnum" type="number" placeholder="Type Here">
  <br>
  <br>
  <input type="submit" value="Enter">
</form>
<?php
} // end not submitted
?>

这篇关于基于html表单输入并显示在html页面中显示从数据库中提取的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆