PHP - 搜索数据库并在同一页面返回结果 [英] PHP - Search database and return results on the same page

查看:77
本文介绍了PHP - 搜索数据库并在同一页面返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常感谢您的时间。

我正在为我的业务创建一个CRM。这将是非常基础的,但包括我的行业在CRM中特别需要的一些关键功能,似乎并不存在于任何地方,但我离题了。



我目前正在使用搜索功能搜索客户端,并在搜索表单下方的下拉菜单中返回结果。当在该列表中选择客户时,用户被重定向到显示与该客户相关的所有信息的页面。



我的问题与此搜索功能有关。我现在有搜索在下拉菜单中将结果作为ECHO返回,它看起来非常混乱。 PHP最终埋在了html表单中。必须有一种更简单更方便的方式来将结果作为列表返回。



侧注:返回的搜索结果甚至不必位于下拉列表中,随着时间的推移,我刚刚找到了这个解决方案,因为它让我可以很容易地将选定的用户传递到下一页的下一个PHP代码,并为ID隐藏表单字段。



这是我迄今为止所做的。有人可以帮我清理它吗?

 <!DOCTYPE html> 
< html>
< head>
< title>客户搜寻结果< / title>
< link rel =stylesheethref =styles.css>
< / head>

< body>

< div class =container>
< form id =contactaction =method =get>

< fieldset>
< h4>搜寻客户< / h4>
< input name =termplaceholder =在此处输入姓名type =text>
< / fieldset>

< fieldset>
< button type =submit>搜寻< /按钮>
< / fieldset>

< / form>
< / div>

< div class ='container'>
< form id ='contact'action ='edit.php'method ='post'>

< fieldset>
< h4>搜寻结果< / h4>
< select size =5style =width:100%name ='id'>

<?php
//数据库连接字符串
include(../../ comm / comm.php);
$ con = mysql_connect($ DB_HOST,$ DB_USER,$ DB_PASS);
if(!$ con)
{
die('Could not connect:'。mysql_error());
}
mysql_select_db($ DB_NAME,$ con);

//检索搜索的字词并显示结果
if(!empty($ _ GET ['term'])){
$ term = mysql_real_escape_string($ _ GET ['术语']);
$ sql =SELECT * FROM client WHERE firstname LIKE'%。$ term。%';
$ r_query = mysql_query($ sql);
while($ row = mysql_fetch_array($ r_query)){

echo< option;
echovalue =';
echo。$ row ['client_id'];
echo'>;
echo。$ row ['firstname'];
echo。$ row ['lastname'];
echo - 。$ row ['city'];
echo,。$ row ['state'];
回显< / option>;

}}
?>

< / select>
< / fieldset>

< fieldset>
< button type ='submit'name ='submit'>查看选择< / button>
< / fieldset>

< / form>
< div>

< / body>
< / html>


解决方案

你的选择可以写成如下: p>

  echo'< option value =$ row [client_id]> $ row [firstname] $ row [lastname]  -  $行[city],$ row [state]< / option>'; 

另外请注意,mysql_函数已被弃用,并以错误的方式使用可能非常危险,你的网站很脆弱。



使用mysqli或PDO准备的语句。​​


Many thanks in advance for your time.

I am in the process of creating a CRM for my business. It will be pretty basic, but include some key features my industry specifically needs in a CRM and that does not seem to exist anywhere already, but I digress.

I am currently working on a Search function in the web app that searches for clients and returns the results in a dropdown menu right below the search form. When a client is selected in that list, the user is redirected to a page that displays all of the info related to that client.

My question is related to this search function. I currently have the search returning the results as an ECHO within the dropdown and it just looks horribly messy. The PHP ends up buried in the html form. There must be an easier and neater way to get the results to return as a list.

SIDE NOTE: the returned search results don't even have to be in a dropdown, I've just come to this solution over time, because it allowed me to pass the selected user on to the next PHP code on the next page fairly easily with the hidden form field for the ID.

Here is what I have going on so far. Can someone help me clean this up?

<!DOCTYPE html>
<html>
<head>
<title>Client Search Results</title>
<link rel="stylesheet" href="styles.css">
</head>

<body>

<div class="container">      
<form id="contact" action="" method="get">

<fieldset>
<h4>Search For Client</h4>
<input name="term" placeholder="Enter Name Here" type="text">
</fieldset>

<fieldset>
<button type="submit">Search</button>
</fieldset>

</form>
</div>

<div class='container'>    
<form id='contact' action='edit.php' method='post'>

<fieldset>
<h4>Search Results</h4>
<select size="5" style="width:100%" name='id' >

<?php
// Database Connection String
include("../../comm/comm.php");
$con = mysql_connect($DB_HOST,$DB_USER,$DB_PASS);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($DB_NAME, $con);

//Retrieve The Searched Term and Display The Results
if (!empty($_GET['term'])) {
$term = mysql_real_escape_string($_GET['term']);     
$sql = "SELECT * FROM client WHERE firstname LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 
while ($row = mysql_fetch_array($r_query)) { 

echo "<option";
echo " value='";
echo "".$row['client_id'];
echo"'>";
echo "".$row['firstname'];
echo " ".$row['lastname'];
echo " - ".$row['city'];
echo " ,".$row['state'];
echo "</option>";

}}
?>

</select>
</fieldset>

<fieldset>
<button type='submit' name='submit'>View Selection</button>
</fieldset>

</form>
<div>

</body>
</html>

解决方案

Your option could just be written as follows:

echo '<option value="$row[client_id]">$row[firstname] $row[lastname] - $row[city], $row[state]</option>';

Also, note that mysql_ functions has been deprecated and used in the wrong way can be very dangerous, leaving your website vulnerable.

Use prepared statements using mysqli or PDO.

这篇关于PHP - 搜索数据库并在同一页面返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆