我正在尝试使用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>
代码之后,但它无法正常工作。
答案 0 :(得分:0)
请看这个 将您的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>