到目前为止我所做的是将下拉选择的数据存储到数据库中,我现在想要的是基于在文本框中输入的用户名并单击搜索按钮,要选择的下拉项目是存储在中的数据数据库以及用户可以根据需要选择的值列表。
我只想知道如何用来自数据库的选定数据替换文本 Select from here 以及允许用户在下拉列表中根据需要编辑数据并在单击提交按钮后再次将其保存在数据库中.
代码同时存储数据如下
<?php
if( $_POST['registerbtn']){
$getuser= mysql_real_escape_string(trim($_POST['user']));
$selected_value = $_POST['selectID'];
if($selected_value != 0){
require("./connect.php");
$query=mysql_query("SELECT * FROM user_details WHERE username='$getuser'");
$numrows= mysql_num_rows($query);
if($numrows == 0) {
mysql_query("INSERT INTO user_details VALUES ( '','$getuser','$selected_value')");
}
else
$errormsg = "There is already a user with that username ." ;
mysql_close();
}
else
$errormsg = "You must select a linecard";
}
//code to fill dropdown
require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
$options = "<option value = '0'> Select from here </option>";
while($rownw = mysql_fetch_array($rs)){
$options .= "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> ";
}
mysql_close() ;
$form = "<form action='./reg.php' method='post'>
<table>
<tr>
<td> </td>
<td> <font color='red'> $errormsg </font> </td>
</tr>
<tr>
<td> Username </td>
<td> <input type='text' name='user' value='$getuser' /> </td>
</tr>
<tr>
<td> Select Linecard </td>
<td> <Select name='selectID' > $options </select> </td>
</tr>
<tr>
<td> </td>
<td> <input type='submit' name='registerbtn' value='Register' /> </td>
</tr>
</form>";
echo $form;
?>
根据在文本框中写入的用户名从数据库中检索数据到下拉列表中的代码如下
<?php
if( $_POST['savebtn']){
$search = $_POST['search'];
if($search == '' )
{
$errormsg1 = "Enter something in the search box";
}
else
{
require("./connect.php");
$query = mysql_query("SELECT * FROM user_details WHERE username = '$search'");
$numrows= mysql_num_rows($query);
$rows=mysql_fetch_assoc($query);
if($numrows == 1)
{
$getuser = $rows['username'];
$select_id= $rows['linecard_id'];
//selection table where linecard_name is stored along with select_id for the linecard_name to be displayed
$query1 = mysql_query("SELECT * FROM selection WHERE select_id = '$select_id' ") ;
$rows1=mysql_fetch_assoc($query1);
$linecard_name= $rows1['linecard_name'];
echo $linecard_name;
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
while($rownw = mysql_fetch_array($rs)){
if( $select_id == $rownw['select_id']) {
echo $select_id;
$options .= "<option value = ". $select_id ." > " .$linecard_name. " </option> ";
}
}
}
else
$errormsg1 = "Username was Not found";
mysql_close() ;
}
}
?>
<?php
$form ="<form action='ad.php' method='post' id='search' >
<div class='place'>
<tr>
<td> </td>
<td> <font color='red' > $errormsg1 </font> </td>
</tr>
<div class='frm2'> <input type='text' size='15' value='$search' name='search' /> </div>
<div class='frm3'> <input type='submit' name='savebtn' /> </div>
<div class='clear'> </div>
</div>
</form>";
echo $form;
?>
<?php
require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection "."ORDER BY linecard_name";
$rs = mysql_query($sql);
$options = "<option value = '0'> Select from here </option>";
while($rownw = mysql_fetch_array($rs)){
$options .= "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> ";
}
mysql_close() ;
$form = "<form action='./reg.php' method='post'>
<table>
<tr>
<td> </td>
<td> <font color='red'> $errormsg </font> </td>
</tr>
<tr>
<td> Username </td>
<td> <input type='text' name='user' value='$getuser' /> </td>
</tr>
<tr>
<td> Select Linecard </td>
<td> <Select name='selectID' > $options </select> </td>
</tr>
<tr>
<td> </td>
<td> <input type='submit' name='savebtn' value='Save' /> </td>
</tr>
</form>";
echo $form;
?>