我到目前为止所做的是将下拉选择的数据存储到数据库中,我现在想要的是基于在文本框中键入的用户名并单击搜索按钮,要选择的下拉项是存储在数据库中的数据以及用户应该能够根据自己的意愿选择的值列表。
我只想知道如何替换文本从此处选择来自数据库的选定数据,以及允许用户在下拉列表中按需要编辑数据,并在点击后再次将其保存在数据库中提交按钮。
代码存储数据时如下
<?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;
?>
答案 0 :(得分:0)
请按以下方式更改您的代码:
<?php
require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
echo "<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' /> </td>
</tr>
<tr>
<td> Select Linecard </td>
<td>";
?>
<Select name='selectID'>
<option value= '0' > Select from here </option>
<?php
while($rownw = mysql_fetch_array($rs)){
echo "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> ";
}
?>
</select>
</tr>
<tr>
<td> </td>
<td> <input type='submit' name='registerbtn' value='Register' /> </td>
</tr>
</table>
</form>
<?php
mysql_close() ;
?>
答案 1 :(得分:0)
您可以将选项存储到变量中,让我们将其命名为$ options,然后将其插入到$ 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' /> </td>
</tr>
<tr>
<td> Select Linecard </td>
<td> <Select name='selectID'> $options </select>
</tr>
<tr>
<td> </td>
<td> <input type='submit' name='registerbtn' value='Register' /> </td>
</tr>
</table>
</form>";
echo $form;
答案 2 :(得分:0)
尝试这样做
<?php
require("./connect.php");
$sql = "SELECT select_id, linecard_name FROM selection ". "ORDER BY linecard_name";
$rs = mysql_query($sql);
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<table>
<tr>
<td> </td>
<td> <font color='red'> $errormsg </font> </td>
</tr>
<tr>
<td> Username </td>
<td> <input type='text' name='user' /> </td>
</tr>
<?php while($rownw = mysql_fetch_array($rs)){ ?>
<tr>
<td> Select Linecard </td>
<td> <Select name='selectID'> <?php echo "<option value = ".$rownw['select_id']." > ".$rownw['linecard_name']. " </option> "; ?> </select>
</tr>
<?php } //end of while loop ?>
<tr>
<td> </td>
<td> <input type='submit' name='registerbtn' value='Register' /> </td>
</tr>
</table>
</form>;