Php 组合框未更新
我有两个php页面,admin_bkp.php和db_submit.php php为用户提供了一个表,让用户为员工设置权限,并将权限保存到名为rights的MySQL表中。员工信息从hs\u hr\u employee中收集,并保存到rights tog中,并带有一个额外的“权限”列 它开始工作时,您可以保存数据,当您刷新页面时,admin_bkp.php会显示您之前选择的组合框中的默认值 但是,现在它返回默认值为'All',这是我不想要的。我希望它显示以前选择的值 单击“保存”按钮后,admin_php.bkp将保存值 代码如下: admin_bkp.phpPhp 组合框未更新,php,mysql,list,combobox,Php,Mysql,List,Combobox,我有两个php页面,admin_bkp.php和db_submit.php php为用户提供了一个表,让用户为员工设置权限,并将权限保存到名为rights的MySQL表中。员工信息从hs\u hr\u employee中收集,并保存到rights tog中,并带有一个额外的“权限”列 它开始工作时,您可以保存数据,当您刷新页面时,admin_bkp.php会显示您之前选择的组合框中的默认值 但是,现在它返回默认值为'All',这是我不想要的。我希望它显示以前选择的值 单击“保存”按钮后,admi
<?php
$connection = mysql_connect('localhost','admin','root');
if( isset($_POST['submit']) )
{
if( isset( $_POST['cb_change'] ) && is_array( $_POST['cb_change'] ))
{
foreach( $_POST['cb_change'] as $emp_number => $permission)
{
echo "$permission";
$sql = "UPDATE `rights` SET Permission='".mysql_real_escape_string($permission)."' WHERE emp_number='".mysql_real_escape_string($emp_number)."'";
echo __LINE__.": sql: {$sql}\n";
mysql_query( $sql );
}
}
}
?>
<p style="text-align: center;">
<span style="font-size:36px;"><strong><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span style="color: rgb(0, 128, 128);">File Database - Administration Panel</span></span></strong></span></p>
<p style="text-align: center;">
</p>
<head>
<style type="text/css">
/*
http://icant.co.uk/csstablegallery/index.php?css=71
Data Tables and Cascading Style Sheets Gallery
Title: Casablanca
Author: RODrigo CASTilho Galv?o Ferreira - RODCAST
URL: http://www.rodcast.com.br
Update: 04/04/2008 10:51 AM
*/
body{
background: url('bg.gif');
}
table {
color: #666;
font: 0.8em/1.6em "Trebuchet MS",Verdana,sans-serif;
border-collapse: collapse;
background: #fff;
}
table,caption {
margin: 0 auto;
border-right: 0px solid #CCC;
border-left: 0px solid #CCC;
}
caption,th,td {
border-left: 0;
padding: 10px;
}
caption,thead th,tfoot th,tfoot td {
background-color: #216dab;
color: #FFF;
font-weight: bold;
text-transform: uppercase
}
thead th {
background-color: #666;
color: #fff;
text-align: center
}
tbody th {
padding: 20px 10px
}
tbody tr.odd {
background-color: #F7F7F7;
color: #666;
}
tbody a {
padding: 1px 2px;
color: #fff;
text-decoration: none;
border-bottom: 1px dotted #E63C1E;
background: #7bafd9;
}
tbody a:active,tbody a:hover,tbody a:focus,tbody a:visited {
color: #fff;
}
tbody tr:hover {
background-color: #EEE;
color: #333
}
tbody tr:hover a {
/*background-color: #FFF*/
color:fff;
}
tbody td+td+td+td a {
color: #fff;
font-weight: bold;
border-bottom: 0;
}
tbody td+td+td+td a:active,tbody td+td+td+td a:hover,tbody td+td+td+td a:focus,tbody td+td+td+td a:visited {
color: #fff
}
tbody td a[href="http://www.rodcast.com.br/"] {
margin: 0 auto;
display: block;
width: 15px;
height: 15px;
background: transparent url('data:image/gif;base64,R0lGODlhDwAPAIAAACEpMf///yH5BAAAAAAALAAAAAAPAA8AAAIjjA8Qer0JmYvULUOlxXEjaEndliUeA56c97TqSD5pfJnhNxYAOw%3D%3D') no-repeat;
text-indent: -999em;
border-bottom: 0;
}
tbody a:visited:after {
font-family: Verdana,sans-serif;
content: "\00A0\221A"
}
</style>
</head>
<form method="post" action="db_submit.php">
<?php
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('users', $connection);
#mysql_query('INSERT into rights(Emp_num, ID, Name, Surname) SELECT emp_number, employee_id, emp_firstname, emp_lastname FROM hs_hr_employee');
$result = mysql_query("SELECT emp_number, employee_id, emp_firstname, emp_lastname, Permissions FROM rights");
mysql_query("INSERT INTO rights (emp_number, employee_id, emp_firstname, emp_lastname)
SELECT emp_number, employee_id, emp_firstname, emp_lastname
FROM hs_hr_employee
ON DUPLICATE KEY UPDATE employee_id = VALUES(employee_id), emp_number = VALUES(emp_number)
");
$duplicates = mysql_query("SELECT emp_number, employee_id, emp_firstname, emp_lastname, count(*) FROM rights GROUP BY emp_number, employee_id, emp_firstname, emp_lastname having count(*) > 1");
$count = mysql_num_rows($duplicates);
if ($count > 0) {
while ($row = mysql_fetch_assoc($duplicates)) {
$field = $row["emp_number"];
$limit = $row["count(*)"] - 1;
mysql_query("DELETE FROM rights WHERE emp_number='$field' LIMIT $limit");
}
mysql_free_result($duplicates);
}
echo "<center>";
/*
echo "
<table >
<tr>
<th>Employee Number</th>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Permissions</th>
<th>Change</th>
</tr>"; */
echo "
<table >
<tr>
<th>Employee Number</th>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Permissions</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['emp_number'] . "</td>";
echo "<td>" . $row['employee_id'] . "</td>";
echo "<td>" . $row['emp_firstname'] . "</td>";
echo "<td>" . $row['emp_lastname'] . "</td>";
#echo "<td>" . $row['Permissions'] . "</td>";
echo "<td>
<select>
<option value='All'>All</option>
<option value='Remote Gaming' selected='selected'>Remote Gaming</option>
<option value='Landbased Gaming'>Landbased Gaming</option>
<option value='General Gaming'>General Gaming</option>
</select>
</td>
";
echo "</tr>" ;
echo "<input type='hidden' name='emp_id[]' value='". $row['employee_id'] ."'>";
}
$selected = $row['Permissions'];
$options = array(
"Remote Gaming",
"Landbased Gaming",
"General Gaming"
);
foreach ($options as $option){
if($option == $selected){
echo "<option value='$option' selected='selected'>$option</option>";
}else{
echo "<option value='$option'>$option</option>";
}
}
#echo "<td>" . $row['Change'] . "</td>";
echo "</table>
<p style='text-align: right;'>
<input name='Save_Btn' type='Submit' value='Save' />
</p>
</center>";
mysql_close($connection);
?>
<p style="text-align: center;">
</p>
<p style="text-align: center;">
</p>
</form>
如果数据库中显示了正确的值,则问题在于在另一端显示数据。据我所知,您希望在html选择框中输出权限列表,当前存储的权限设置为该用户的“已选择”,对吗
如果是,请在此处输入您的代码:
echo "<td> <select name='cb_change[]' SELECTED VALUE=".$row['Permissions'].">
<option value='All'>All</option>
<option value='Remote Gaming'>Remote Gaming</option>
<option value='Landbased Gaming'>Landbased Gaming</option>
<option value='General Gaming'>General Gaming</option>
</select></td>";
echo”
全部的
远程游戏
陆地游戏
一般游戏
";
这是错误的。您需要将
值与当前选择的值相匹配,然后将属性粘贴在其上
例如:
$selected=$row['Permissions'];
$options=array(
“远程游戏”,
“陆地游戏”,
“一般游戏”
);
foreach($options作为$option){
如果($option==$selected){
回显“$选项”;
}否则{
回显“$选项”;
}
}
这应该行得通
最终编辑
以下标有“代码”的代码应放在下面:
echo "
<table >
<tr>
<th>Employee Number</th>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Permissions</th>
</tr>";
echo”
员工编号
身份证件
名称
姓
权限
";
代码:
$options=array(
“全部”,
“远程游戏”,
“陆地游戏”,
“一般游戏”
);
while($row=mysql\u fetch\u数组($result))
{
回声“;
回显“$row['emp_number']”;
回显“$row['employee_id']”;
回显“$row['emp_firstname']”;
回显“$row['emp_lastname']”;
#回显“$row['Permissions']”;
回声“;
foreach($options作为$option){
如果($option==$row['Permissions']){
回显“$选项”;
}否则{
回显“$选项”;
}
}
回声“;
回声“;
回声“;
}//结束时
所以我基本上用您给我的代码替换了这段代码?我尝试的是这个,但它显示了多个组合框,这是我不想要的。我想要的是一个组合框,默认情况下显示先前选择的选项。我更新了主要问题中的代码,让您看看我做了什么。我给您的代码循环遍历每个权限(存储在数组中),并为每个权限输出一个元素。您应该将代码包装在
中,如编辑中所示。我不确定您从何处获得此信息:SELECTED VALUE=“.$SELECTED.”
。它看起来像是您试图在html中使用的某种SQL查询?据我所知,这在这种情况下没有任何作用,是无效的。我再次更新了代码,向您展示了我所做的工作。好的,请参阅EDIT2,了解我给您的代码正在做什么。有可能我完全误解了你的问题,我随机回答了一些问题,但看看我做了什么。
echo "<td> <select name='cb_change[]' SELECTED VALUE=".$row['Permissions'].">
<option value='All'>All</option>
<option value='Remote Gaming'>Remote Gaming</option>
<option value='Landbased Gaming'>Landbased Gaming</option>
<option value='General Gaming'>General Gaming</option>
</select></td>";
$selected = $row['Permissions'];
$options = array(
"Remote Gaming",
"Landbased Gaming",
"General Gaming"
);
foreach ($options as $option){
if($option == $selected){
echo "<option value='$option' selected='selected'>$option</option>";
}else{
echo "<option value='$option'>$option</option>";
}
}
echo "
<table >
<tr>
<th>Employee Number</th>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Permissions</th>
</tr>";
$options = array(
"All",
"Remote Gaming",
"Landbased Gaming",
"General Gaming"
);
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['emp_number'] . "</td>";
echo "<td>" . $row['employee_id'] . "</td>";
echo "<td>" . $row['emp_firstname'] . "</td>";
echo "<td>" . $row['emp_lastname'] . "</td>";
#echo "<td>" . $row['Permissions'] . "</td>";
echo "<td> <select name='cb_change[]'>";
foreach($options as $option){
if($option == $row['Permissions']){
echo "<option value='" . $option . "' selected='selected'>". $option ."</option>";
}else{
echo "<option value='" . $option . "'>". $option ."</option>";
}
}
echo "</select> </td>";
echo "</tr>" ;
echo "<input type='hidden' name='emp_id[]' value='". $row['employee_id'] ."'>";
} // end while