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

piwo6bdm  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(224)

我正在尝试用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> 但它不起作用。

nx7onnlm

nx7onnlm1#

请看这个将你的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>

相关问题