php - 从数据库中获取同一页面中的结果

标签 php html mysql

我正在尝试使用 PHP 和 MySQL 使用搜索方法从数据库中获取数据。它将在另一个页面中显示结果。如何在同一页面中获得此结果。

HTML脚本

<html>
<head>
<title>Digital Library</title>
</head>    
<body>
<br />
<center>
<img src="logo.png"></img>
<h1>Digital Library</h1>
<h3>Enter Comapany Name</h3>
<form action="search.php" method="post">
<input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" align="center" />
</form>
</center>
</body>
</html>

这是 PHP脚本

<?php
mysql_connect ("localhost", "root","")  or die (mysql_error());
mysql_select_db ("vdl");

$term = $_POST['term'];

$sql = mysql_query("select * from digital_library where company_name like '%$term%'");

while ($row = mysql_fetch_array($sql)){
    Print "<strong>Comapny Name:</strong> ".$row['company_name']."<br>";
    Print "<strong> Since: </strong> ".$row['since']."<br>";
    Print "<strong> Strength: </strong> ".$row['strength']."<br>";
    Print "<strong> Head Quarters: </strong> ".$row['head_quarter']."<br>";
    Print "<strong> Location (City): </strong> ".$row['locations']."<br>";
    Print "<strong> Development Centers: </strong> ".$row['development_centers']."<br>";
    Print "<strong> Customers: </strong> ".$row['customers']."<br>";
    Print "<strong> MNC: </strong> ".$row['mnc']."<br>";
    Print "<strong> CMMI: </strong> ".$row['cmmi']."<br>";
    Print "<strong> Domains: </strong> ".$row['domains']."<br>";
    Print "<strong> Industries: </strong> ".$row['industries']."<br>";
    Print "<strong> Domain Competitors: </strong> ".$row['domain_competitor']."<br>";
    Print "<strong> Products: </strong> ".$row['products']."<br>";
    Print "<strong> Services: </strong> ".$row['services']."<br>";
    Print "<strong> Uniqueness: </strong> ".$row['uniqueness']."<br>";
    Print "<strong> URL: </strong> ".$row['url']."<br>";
    Print "<strong> Onsights: </strong> ".$row['onsight']."<br>";
    Print "<strong> Benefits: </strong> ".$row['benefits']."<br>";
    Print "<strong> Awards: </strong> ".$row['awards']."<br>";
    echo '<br/><br/>';
    }
?>

我尝试将代码放入 <body> 之后标记,但它不起作用。

最佳答案

请看这里 将您的 html 文件保存为 .php 并在此文件中添加您的 php 代码

     <?php
    $row = '';
    if(isset($_POST['term']) && !empty($_POST['term'])){
        mysql_connect ("localhost", "root","")  or die (mysql_error());
        mysql_select_db ("vdl");

        $term = $_POST['term'];

        $sql = mysql_query("select * from digital_library where company_name like '%$term%'");
$row = mysql_fetch_array($sql);
    }

        ?>
            <html>
            <head>
            <title>Digital Library</title>
            </head>    
            <body>
            <br />
            <center>
            <img src="logo.png"></img>
            <h1>Digital Library</h1>
            <h3>Enter Comapany Name</h3>
            <form action="search.php" method="post">
            <input type="text" name="term" /><br />
            <input type="submit" name="submit" value="Submit" align="center" />
            </form>
            </center>


             <?php
        if(!empty($row)){
         while ($row){
                Print "<strong>Comapny Name:</strong> ".$row['company_name']."<br>";
                Print "<strong> Since: </strong> ".$row['since']."<br>";
                Print "<strong> Strength: </strong> ".$row['strength']."<br>";
                Print "<strong> Head Quarters: </strong> ".$row['head_quarter']."<br>";
                Print "<strong> Location (City): </strong> ".$row['locations']."<br>";
                Print "<strong> Development Centers: </strong> ".$row['development_centers']."<br>";
                Print "<strong> Customers: </strong> ".$row['customers']."<br>";
                Print "<strong> MNC: </strong> ".$row['mnc']."<br>";
                Print "<strong> CMMI: </strong> ".$row['cmmi']."<br>";
                Print "<strong> Domains: </strong> ".$row['domains']."<br>";
                Print "<strong> Industries: </strong> ".$row['industries']."<br>";
                Print "<strong> Domain Competitors: </strong> ".$row['domain_competitor']."<br>";
                Print "<strong> Products: </strong> ".$row['products']."<br>";
                Print "<strong> Services: </strong> ".$row['services']."<br>";
                Print "<strong> Uniqueness: </strong> ".$row['uniqueness']."<br>";
                Print "<strong> URL: </strong> ".$row['url']."<br>";
                Print "<strong> Onsights: </strong> ".$row['onsight']."<br>";
                Print "<strong> Benefits: </strong> ".$row['benefits']."<br>";
                Print "<strong> Awards: </strong> ".$row['awards']."<br>";
                echo '<br/><br/>';
                }
        }
        ?>
            </body>
            </html>

关于php - 从数据库中获取同一页面中的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50302957/

相关文章:

php - 如果存在唯一 ID,则更新 MySQL 中的多个列

PHP 发票引用 ID 自动递增

html - 跨度内的标签 p 不应用 css 样式

mysql - 姓名按降序排列如果两个人的姓名相同,则按id升序排列

mysql - 无法在 Mac 上重置 MySQL root 密码

php - DataTables 第 2 页的分页未调用放大弹出窗口

php - 功能类似于 ctype_alpha 但带有特殊字母

javascript - 控制台中出现不兼容的 IE 文档模式错误?

c# - 将 CssClass 分配给 MasterPage 中的 LinkBut​​ton

mysql - SQL中左连接和右连接的原因是什么