drd0833 2018-05-12 04:54 采纳率: 100%
浏览 68
已采纳

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

I am trying with PHP and MySQL Get data from database using search methods. It will show result in another page. How to get this result in same page.

This HTML Script

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

This is PHP Script

<?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/>';
    }
?>

I am tried with put code into after <body> tag but it won't work.

  • 写回答

1条回答 默认 最新

  • doujue9767 2018-05-12 05:05
    关注

    Please see this save your html file as .php and add your php code in this file

         <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站