简体   繁体   English

PHP-搜索数据库并在同一页面上返回结果

[英]PHP - Search database and return results on the same page

Many thanks in advance for your time. 非常感谢您的宝贵时间。

I am in the process of creating a CRM for my business. 我正在为自己的业务创建CRM。 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. 这将是非常基本的,但是包括我的行业在CRM中特别需要的一些关键功能,而且似乎还不存在,但是我离题了。

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. 目前,我在下拉菜单中以ECHO的形式返回搜索结果,结果看起来非常混乱。 The PHP ends up buried in the html form. PHP最后以html形式被掩埋。 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. 旁注:返回的搜索结果甚至不必放在下拉列表中,我只是随着时间的推移而采用了此解决方案,因为它使我可以轻松地将所选用户传递给下一页上的下一个PHP代码。 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. 另外,请注意,不建议使用mysql_函数,并以错误的方式使用它会非常危险,从而使您的网站容易受到攻击。

Use prepared statements using mysqli or PDO. 使用通过mysqli或PDO准备的语句。

...
$r_query = mysql_query($sql); 
while ($row = mysql_fetch_array($r_query)) { ?>

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

<?php  }}  ?>

Or some variation of that. 或一些变化。 Point is, your PHP doesn't have to be a continuous block, you can close your PHP tag at any time, resume using regular HTML, and then open a new PHP tag and continue in your loop. 关键是,您的PHP不必是连续的块,您可以随时关闭PHP标记,使用常规HTML恢复,然后打开一个新的PHP标记并继续循环。

Also, in the above example, <?= is shorthand for <?php echo . 另外,在上面的示例中, <?=<?php echo简写。

Also, in your example, you're using mysql_ functions, which have been deprecated in later versions of PHP5 and removed in PHP7. 另外,在您的示例中,您使用的是mysql_函数,该函数在更高版本的PHP5中已弃用,在PHP7中已删除。 Best to study up on mysqli_ or PDO (which you can also use with MySQL databases). 最好学习mysqli_PDO (您也可以将它们与MySQL数据库一起使用)。

Lastly, once you start using either of those, look into prepared statements, which will make your code function better/avoid SQL injections. 最后,一旦开始使用其中任何一种,请查看准备好的语句,这将使您的代码功能更好/避免使用SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM