我对PHP编码有一点疑问,请帮帮我。实际上我正在显示当前的作业,搜索后,结果将显示在同一页面中。已完成,但结果显示在内容下方。我只需显示该页面的结果?我想让之前的内容无法使用。这是代码:
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>Job Openings</h2>
<form method="POST" action="jobopenings.php">
<input type="text" name="txt" required />
<input type="submit" name="btn" value="search" /><br/>
</form>
</body>
</html>
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');
$con = mysqli_connect(HOST,USER,PASS,DB) or die("Unable to connect to db");
$selQ = "Select * from jobpostings";
$res1 = mysqli_query($con, $selQ);
while($row = mysqli_fetch_array($res1))
echo $row[3]."<br> Job Id:<b>".$row[2]."</b><br><b>".$row[1]."</b>"."
<br>"."Exp:".$row[5]."<br>"."Location:".$row[6]."<br>".$row[8]."<br><br>";
if(isset($_POST["btn"])){
$query=$_POST['txt'];
$query=htmlspecialchars($query);
$query=mysqli_real_escape_string($con,$query);
$raw_results="select * from jobpostings where (C_name like '%" .$query."%')
or (Job_title like '%".$query."%')";
$final_results=mysqli_query($con,$raw_results);
if(mysqli_num_rows($final_results)>0){
while($results=mysqli_fetch_array($final_results)){
echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]
<br>".$results[3]."</p>";
}
}
else{
echo '<b style="color:red;">No results found</b>';
}
}
?>
提前致谢
答案 0 :(得分:0)
将整个php代码放在<body>
中,并将if条件显示为任何部分。像这样:
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>Job Openings</h2>
<?php if(!isset($_POST["btn"]))
{ ?>
<form method="POST" action="jobopenings.php">
<input type="text" name="txt" required />
<input type="submit" name="btn" value="search" /><br/>
</form>
<?php }
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');
$con = mysqli_connect(HOST,USER,PASS,DB) or die("Unable to connect to db");
$selQ = "Select * from jobpostings";
$res1 = mysqli_query($con, $selQ);
while($row = mysqli_fetch_array($res1))
echo $row[3]."<br> Job Id:<b>".$row[2]."</b><br><b>".$row[1]."</b>"."
<br>"."Exp:".$row[5]."<br>"."Location:".$row[6]."<br>".$row[8]."<br><br>";
if(isset($_POST["btn"])){
$query=$_POST['txt'];
$query=htmlspecialchars($query);
$query=mysqli_real_escape_string($con,$query);
$raw_results="select * from jobpostings where (C_name like '%" .$query."%')
or (Job_title like '%".$query."%')";
$final_results=mysqli_query($con,$raw_results);
if(mysqli_num_rows($final_results)>0){
while($results=mysqli_fetch_array($final_results)){
echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]
<br>".$results[3]."</p>";
}
}
else{
echo '<b style="color:red;">No results found</b>';
}
}
?>
</body>
</html>
答案 1 :(得分:0)
以下是修改后的代码:
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');
$con = mysqli_connect(HOST,USER,PASS,DB) or die("Unable to connect to db");
$final_results = null;
if(isset($_POST["btn"])){
$query=$_POST['txt'];
$query=htmlspecialchars($query);
$query=mysqli_real_escape_string($con,$query);
$raw_results="select * from jobpostings where (C_name like '%" .$query."%') or (Job_title like '%".$query."%')";
$final_results=mysqli_query($con,$raw_results);
}
?>
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>Job Openings</h2>
<form method="POST" action="jobopenings.php">
<input type="text" name="txt" required />
<input type="submit" name="btn" value="search" /><br/>
</form>
<?php
if(!is_null($final_results) && mysqli_num_rows($final_results)>0){
while($results=mysqli_fetch_array($final_results)){
echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]."<br>".$results[3]."</p>";
}
}else{
echo '<b style="color:red;">No results found</b>';
}
$selQ = "Select * from jobpostings";
$res1 = mysqli_query($con, $selQ);
while($row = mysqli_fetch_array($res1))
echo $row[3]."<br> Job Id:<b>".$row[2]."</b><br><b>".$row[1]."</b>"."
<br>"."Exp:".$row[5]."<br>"."Location:".$row[6]."<br>".$row[8]."<br><br>";
?>
</body>
</html>
我没有测试此代码,请确认它正常。
也始终在<html>
标记之前启动php代码,您必须打印的所有内容必须在<body>
标记内
答案 2 :(得分:0)
1)我建议你不要创建2个SQL查询(一个没有搜索,一个没有搜索)。您可以根据搜索进行一次查询并附加where子句。
2)“ ..但结果显示在内容下方。”=&gt;将所有内容包装在<body>
标记内。
更新代码
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>Job Openings</h2>
<form method="POST" action="jobopenings.php">
<input type="text" name="txt" required />
<input type="submit" name="btn" value="search" /><br/>
</form>
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');
$con = mysqli_connect(HOST, USER, PASS, DB) or die("Unable to connect to db");
$where = "";
$searched = false;
if (isset($_POST["btn"]) && isset($_POST['txt'])) {
$searched = true;
$txt = htmlspecialchars($_POST['txt']);
$txt = mysqli_real_escape_string($con, $txt);
$where = "WHERE (C_name like '%" .$txt. "%') or (Job_title like '%" .$txt. "%')";
}
$raw_results = "SELECT * FROM `jobpostings` ".$where;
$final_results = mysqli_query($con, $raw_results);
if (mysqli_num_rows($final_results) > 0) {
while ($results = mysqli_fetch_array($final_results)) {
if(!$searched){
echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]."<br>".$results[3]."</p>";
} else {
echo $results[3] . "<br> Job Id:<b>" . $results[2] . "</b><br><b>" . $results[1] . "</b>" . "<br>" . "Exp:" . $results[5] . "<br>" . "Location:" . $results[6] . "<br>" . $results[8] . "<br><br>";
}
}
} else {
echo '<b style="color:red;">No results found</b>';
}?>
</body>
</html>
答案 3 :(得分:0)
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>Job Openings</h2>
<form method="POST" action="jobopenings.php">
<input type="text" name="txt" required />
<input type="submit" name="btn" value="search" /><br/>
</form>
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', '');
define('DB', '*****');
$con = mysqli_connect(HOST,USER,PASS,DB) or die("Unable to connect to db");
if(isset($_POST["btn"])){
$query=$_POST['txt'];
$query=htmlspecialchars($query);
$query=mysqli_real_escape_string($con,$query);
$raw_results="select * from jobpostings where (C_name like '%" .$query."%')
or (Job_title like '%".$query."%')";
$final_results=mysqli_query($con,$raw_results);
if(mysqli_num_rows($final_results)>0){
while($results=mysqli_fetch_array($final_results)){
echo "<p><b>".$results[1]."</b><br> Job Id:".$results[2]
<br>".$results[3]."</p>";
}
}
else{
echo '<b style="color:red;">No results found</b>';
}
}
else{
$selQ = "Select * from jobpostings";
$res1 = mysqli_query($con, $selQ);
while($row = mysqli_fetch_array($res1))
echo $row[3]."<br> Job Id:<b>".$row[2]."</b><br><b>".$row[1]."</b>"."
<br>"."Exp:".$row[5]."<br>"."Location:".$row[6]."<br>".$row[8]."<br><br>";}
?>
</body>
</html>