我是堆栈溢出的新手,非常感谢所有的帮助。我目前有一个包含基本列、姓名、公司等的数据库。我创建了一个搜索查询,根据名字、姓氏或时间戳日期在该列表中进行切换。我能够在搜索的查询页面上打印结果,但希望将其作为新条目预先填充在同一表单页面上。我能够从查询页面链接到表单页面,但不确定如何在表单页面上填充这些结果。
我当前的查询页面打印如下:
<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<div id="container" style="width:750px;background-color:#FFFE8D;margin-left: 250px">
<div id="Back" style="float:left; background-color:#FFFE8D;">
<form action="http://localhost/contractor/existingcontractorpage3.php">
<input type="submit" value="Back">
</form>
</div>
<div id="Is this the Contractor?" style="float:right; background-color:#FFFE8D;">
<form action="http://localhost/contractor/redirectcontractorpage.php">
<input type="submit" value="Next">
</form>
</div>
<div id="info" style="width:750px;height:95px; text-align: center">
<h3> If this is the contractor, please move on to the next page using the corresponding button above. </h3>
<h3> Please enter the exact information on the next page. </h3>
</div>
<div id="results" style="width:750px;height:225px; text-align: center">
<?php
$host = "localhost"; //server
$db = ""; //database name
$user = ""; //databases user name
$pwd = ""; //password
mysql_connect($host, $user, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$searchTerm = trim($_GET['searchname']);
// Check if $searchTerm is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
else
{
$sql = "SELECT * FROM contractor WHERE CONCAT(FIRSTNAME,' ',LASTNAME,' ', ARRIVAL) like '%$searchTerm%'";
$query = mysql_query($sql);
$count=mysql_num_rows($query);
if(($count)>=1)
{
$output = "";
while($row = mysql_fetch_array($query))
{
$output .= "First Name: " . $row['FIRSTNAME'] . "<br />";
$output .= "Last Name: " . $row['LASTNAME'] . "<br />";
$output .= "Arrival: " . $row['ARRIVAL'] . "<br />";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
}
?>
</div>
<br> </br>
<br> </br>
</body>
</html>
现在理想情况下,我希望结果在这里弹出,如果可能的话,每个结果的右侧有一个单选按钮(如果有多个)来选择并继续到表单页面以预填充每个字段。表单页面就像这样:
<form action="insert_submit.php" method="post">
First Name: <input type = "text" name="FIRSTNAME" id="FIRSTNAME" />
<br>
</br>
Last Name: <input type = "text" name="LASTNAME" id="LASTNAME"/>
<br>
</br>
Purpose: <input type = "text" name="PURPOSE" id="PURPOSE"/>
<br>
</br>
Company: <input type = "text" name="COMPANY" id="COMPANY" />
<br>
</br>
Who here to see: <input type = "text" name="WHOHERETOSEE" id="WHOHERETOSEE"/>
<br>
</br>
<input type="submit" value="Submit">
<br>
</br>
</form>
非常感谢!希望尽快收到回复,因为这是我项目的最后一根稻草。
最佳答案
执行此操作的最简单方法是像您正在做的那样从数据库中提取数据,除非您要将表单添加到页面中。然后在循环中打印出表单字段:
您需要为每个 radio 指定一个唯一的名称。您可以使用递增的 id,也可以将其指定为与行名称相同的名称。
方法一:
while($row = mysql_fetch_array($query))
{
$output .= "<label>First Name: " . $row['FIRSTNAME'] . "</label><input type=\"radio\" name = \"radio[]\" value=\"".$row['FIRSTNAME']." ".$row['LASTNAME']."\"/>";
..
..
}
提交表单后,您将能够使用 GET 或 POST 从单选按钮获取值。
设置 name = "radio[]"将所有值放入一个数组中,提交表单后您可以在下一页上获取该数组。
在表单提交后的下一页,$_POST['radio'] 或 $_GET['radio'] 将返回您指定的所有值的数组。请注意上面的 value 属性是如何填写的。对每一行执行此操作。
使用 my_sql 连接时也要小心。自 PHP 5.5.0 起已贬值。使用 mysqli 或 PDO 代替,更安全。 http://ie1.php.net/function.mysql-connect
检查此链接。它对两者进行了比较并提供了示例
关于php - 如何根据数据库的搜索结果预填充字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24062654/