PHP 搜索然后插入
PHP SEARCH and then INSERT
在 PHP 方面,我是一个相对的新手,在进行了一些搜索之后,我似乎无法找到问题的答案。
我有的是非常基本的,两个表 'person' 和 'case'。我能够插入和搜索一个人(尽管我花了一些时间才让它工作!)。现在涉及到 'Case' 我需要首先搜索一个人,我希望搜索结果保留在表单中,然后用户将其余的案例信息添加到表单然后提交。我已经尝试将我的两个代码组合在一起,但我仍然没有成功。
即使我能找到一种方法让输出保持填充形式,这对我来说也是向前迈出的一大步。我将输出保留为打印,以便搜索结果显示在页面上。任何帮助将不胜感激。当前的搜索和插入代码如下:
<?php
include 'connection.php';
$output ='';
//collect
if (isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM person WHERE forename LIKE '%$searchq%' OR surname LIKE '%$searchq%'") or die("Could not search");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'There was no search results';
}else{
while($row = mysql_fetch_array($query)) {
$firstname = $row['forename'];
$lastname = $row['surname'];
$output .= '<div>'.$firstname.' '.$lastname.'</div>';
}
}
}
?>
<form action="basicsearch.php" method="post">
<input type="text" name="search" placeholder="Search for Persons..." onkeydown="searchq()1" />
<input type="submit" value=">>"/>
</form>
<?php print("$output");?>
<?php
$forename = $surname = $dateofbirth = $postcode = $addresslineone = $addresslinetwo = $towncity = $contactnumber="";
$forenameErr = $surnameErr = $dateofbirthErr = $postcodeErr = $addresslineoneErr = $addresslinetwoErr = $towncityErr = $contactnumberErr = "";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
if (empty($_POST["forename"])) {
$forenameErr = "*First name is required";
$valid = false;
}
else {
$forename = test_input($_POST["forename"]);
}
if (empty($_POST["surname"])) {
$surnameErr = "*Last name is required";
$valid = false;
}
else {
$surname = test_input($_POST["surname"]);
}
//only testing forename/surname at the moment
if($valid){
include 'datasubmitted.php';
exit;
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<div class="label">*First Name:
<div class="txtbox">
<input name="forename" type="text" id="txt" placeholder="Enter Your First Name." value="<?php echo $surname; ?>"/>
<span class="error"><p></p><?php echo $forenameErr; ?></span>
</div>
</div>
<div class="label">Last Name:
<div class="txtbox">
<input name="surname" type="text" id="txt" placeholder="Enter Your Last Name." value="<?php echo $surname; ?>"/>
<span class="error"><p></p><?php echo $surnameErr; ?>
</div>
</div>
<input type="submit" value="Submit" name="Submit" />
</form>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<div class="label">*First Name:
<div class="txtbox">
<input name="forename" type="text" id="txt" placeholder="Enter Your First Name." value="<?php echo $surname; ?>"/>
<span class="error"><p></p><?php echo $forenameErr; ?></span>
</div>
</div>
<div class="label">Last Name:
<div class="txtbox">
<input name="surname" type="text" id="txt" placeholder="Enter Your Last Name." value="<?php echo $surname; ?>"/>
<span class="error"><p></p><?php echo $surnameErr; ?>
</div>
</div>
<input type="submit" value="Submit" name="Submit" />
</form>
一切顺利,
克里斯
首先,尽管您应用了 preg_replace(),但您的 SQL 代码可能仍然容易受到 SQL 注入的攻击。最好对使用 PHP filter_input 函数净化的输入使用参数化查询。 (与手工构建的输入消毒器相比,这不太可能发生有意义的故障。)
我看不到你的 INSERT 语句。您正在使用 MySQL,因此您可以考虑先执行 INSERT IGNORE,然后执行 SELECT.
但即使是这个建议也未能触及比表面上看起来更复杂的主题的表面。如果你有两个同名客户(名字相同,但不同的人),你会怎么做?您将如何处理潜在的姓名拼写错误以及由此产生的重复记录?一个人能有很多个案子吗?
这些都是系统问题,也是用户界面人体工程学问题。只有精心的用户界面设计和系统设计才能有效克服这些问题!
如果您正在处理一个可能有数千条记录的数据库,您将需要仔细考虑这些问题。如果您 "only" 与数百人打交道;那么这可能不是问题!
请告诉我们更多……
在 PHP 方面,我是一个相对的新手,在进行了一些搜索之后,我似乎无法找到问题的答案。
我有的是非常基本的,两个表 'person' 和 'case'。我能够插入和搜索一个人(尽管我花了一些时间才让它工作!)。现在涉及到 'Case' 我需要首先搜索一个人,我希望搜索结果保留在表单中,然后用户将其余的案例信息添加到表单然后提交。我已经尝试将我的两个代码组合在一起,但我仍然没有成功。
即使我能找到一种方法让输出保持填充形式,这对我来说也是向前迈出的一大步。我将输出保留为打印,以便搜索结果显示在页面上。任何帮助将不胜感激。当前的搜索和插入代码如下:
<?php
include 'connection.php';
$output ='';
//collect
if (isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM person WHERE forename LIKE '%$searchq%' OR surname LIKE '%$searchq%'") or die("Could not search");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'There was no search results';
}else{
while($row = mysql_fetch_array($query)) {
$firstname = $row['forename'];
$lastname = $row['surname'];
$output .= '<div>'.$firstname.' '.$lastname.'</div>';
}
}
}
?>
<form action="basicsearch.php" method="post">
<input type="text" name="search" placeholder="Search for Persons..." onkeydown="searchq()1" />
<input type="submit" value=">>"/>
</form>
<?php print("$output");?>
<?php
$forename = $surname = $dateofbirth = $postcode = $addresslineone = $addresslinetwo = $towncity = $contactnumber="";
$forenameErr = $surnameErr = $dateofbirthErr = $postcodeErr = $addresslineoneErr = $addresslinetwoErr = $towncityErr = $contactnumberErr = "";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
if (empty($_POST["forename"])) {
$forenameErr = "*First name is required";
$valid = false;
}
else {
$forename = test_input($_POST["forename"]);
}
if (empty($_POST["surname"])) {
$surnameErr = "*Last name is required";
$valid = false;
}
else {
$surname = test_input($_POST["surname"]);
}
//only testing forename/surname at the moment
if($valid){
include 'datasubmitted.php';
exit;
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<div class="label">*First Name:
<div class="txtbox">
<input name="forename" type="text" id="txt" placeholder="Enter Your First Name." value="<?php echo $surname; ?>"/>
<span class="error"><p></p><?php echo $forenameErr; ?></span>
</div>
</div>
<div class="label">Last Name:
<div class="txtbox">
<input name="surname" type="text" id="txt" placeholder="Enter Your Last Name." value="<?php echo $surname; ?>"/>
<span class="error"><p></p><?php echo $surnameErr; ?>
</div>
</div>
<input type="submit" value="Submit" name="Submit" />
</form>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<div class="label">*First Name:
<div class="txtbox">
<input name="forename" type="text" id="txt" placeholder="Enter Your First Name." value="<?php echo $surname; ?>"/>
<span class="error"><p></p><?php echo $forenameErr; ?></span>
</div>
</div>
<div class="label">Last Name:
<div class="txtbox">
<input name="surname" type="text" id="txt" placeholder="Enter Your Last Name." value="<?php echo $surname; ?>"/>
<span class="error"><p></p><?php echo $surnameErr; ?>
</div>
</div>
<input type="submit" value="Submit" name="Submit" />
</form>
一切顺利, 克里斯
首先,尽管您应用了 preg_replace(),但您的 SQL 代码可能仍然容易受到 SQL 注入的攻击。最好对使用 PHP filter_input 函数净化的输入使用参数化查询。 (与手工构建的输入消毒器相比,这不太可能发生有意义的故障。)
我看不到你的 INSERT 语句。您正在使用 MySQL,因此您可以考虑先执行 INSERT IGNORE,然后执行 SELECT.
但即使是这个建议也未能触及比表面上看起来更复杂的主题的表面。如果你有两个同名客户(名字相同,但不同的人),你会怎么做?您将如何处理潜在的姓名拼写错误以及由此产生的重复记录?一个人能有很多个案子吗? 这些都是系统问题,也是用户界面人体工程学问题。只有精心的用户界面设计和系统设计才能有效克服这些问题! 如果您正在处理一个可能有数千条记录的数据库,您将需要仔细考虑这些问题。如果您 "only" 与数百人打交道;那么这可能不是问题!
请告诉我们更多……