我有一个html页面,它从'hs_hr_employee'表中获取数据,并将信息放在网页上的表格中。然后我有另一个表'权限',它从'hs_hr_employee'表中的4列获取信息并将它们存储在列中。除了这4个,“权利”表还有一个额外的列“权限”。
现在,我有一个带有4个选项的组合框。当我单击“保存”按钮时,我想将值选择存储在组合框中,并将其保存在与用户相关的“权限”表中。
(每个用户旁边都有一个组合框)。
更新的代码:
<?php
$connection = mysql_connect('localhost','admin','root');
if( isset($_POST['submit']) )
{
if( isset( $_POST['cb_permissions'] ) && is_array( $_POST['cb_permissions'] ))
{
foreach( $_POST['cb_permissions'] as $emp_number => $permission)
{
$sql = "UPDATE `your_permission_table` 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">
table, td, th
{
border:1px solid #666;
font-style:Calibri;
}
th
{
background-color:#666;
color:white;
font-style:Calibri;
}
</style>
</head>
<form method="post" action="admin.php">
<?php
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('users', $connection);
$result = mysql_query("SELECT emp_number, employee_id, emp_lastname, emp_firstname FROM hs_hr_employee");
echo "<center>";
echo "<table >
<tr>
<th>Employee Number</th>
<th>Employee ID</th>
<th>Surname</th>
<th>Name</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_lastname'] . "</td>";
echo "<td>" . $row['emp_firstname'] . "</td>";
echo "<td> <select name='cb_permissions['".$row['emp_number']."'><option value='all'>All</option> <option value='remote'>Remote Gaming</option> <option value='landbased'>Landbased Gaming</option> <option value='general'>General Gaming</option> </select> </td>";
echo "</tr>" ;
}
echo "</table>";
echo "</center>";
echo mysql_query('INSERT into rights(Emp_num, ID, Name, Surname) SELECT emp_number, employee_id, emp_firstname, emp_lastname FROM hs_hr_employee');
$_POST['cb_permissions'];
mysql_close($connection);
?>
<p style="text-align: center;">
</p>
<p style="text-align: center;">
</p>
<p style="text-align: right;">
<input name="Save_Btn" type="button" value="Save" />
</p>
</form>
关于我如何做到的任何帮助?
获取我正在做的基本概念的屏幕截图:
答案 0 :(得分:0)
首先,您应该在文档的最顶部移动连接代码:
$connection = mysql_connect('localhost','admin','root');
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('users', $connection);
接下来,您必须将表格包装在标记内:
<form method="post" action="target_url.php>
<table>
...
</table>
<input type="submit" name="submit" value="Save"/>
</form>
之后,您会在表单的某处存储employee_id
或emp_number
(取决于您将用于设置权限的表格键):
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['emp_number']; ?></td>
<td><?php echo $row['employee_id']; ?></td>
<td><?php echo $row['emp_lastname']; ?></td>
<td><?php echo $row['emp_firstname']; ?></td>
<td><select name="cb_permissions['<?php echo $row['emp_number']; ?>']">
<option value='all'>All</option>
<option value='remote'>Remote Gaming</option>
<option value='landbased'>Landbased Gaming</option>
<option value='general'>General Gaming</option>
</select></td>
</tr>
<?php
}
然后,在target_url.php
上,你必须这样做:
如果
target_url.php
与您的表单相同,则下面的代码应放在您文档的最顶层。
<?php
if( isset($_POST['submit']) )
{
if( isset( $_POST['cb_permissions'] ) && is_array( $_POST['cb_permissions'] ))
{
foreach( $_POST['cb_permissions'] as $emp_number => $permission)
{
$sql = "UPDATE `your_permission_table` SET permission='".mysql_real_escape_string($permission)."' WHERE emp_number='".mysql_real_escape_string($emp_number)."'";
echo __LINE__.": sql: {$sql}\n";
mysql_query( $sql );
}
}
}
?>
就是这样。
答案 1 :(得分:0)
1次你会学习这个,接下来 - 使用。代码将开始组织...... 也许复杂的首发。但这是正确的方式。