获取 ComboBox 变量存储在 MySQL 中
我有一个 html 页面,它从“hs_hr_employee”表获取数据并将信息放置在网页上的表中。然后我有另一个表“rights”,它从“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>
对我如何做到这一点有任何帮助吗?
截图来了解我正在做的事情的基本概念:
I have an html page that gets data from 'hs_hr_employee' table and lays the info in a table on a web page. Then I have another table 'rights' which gets info from 4 columns from the 'hs_hr_employee' table and stores them in columns. In addition to those 4, the 'rights' table has an extra column 'Permissions'.
Now, I have a combobox with 4 options. When I click the 'Save' button I want to store the value select in the combobox and save it in the 'rights' table in relation to the user.
(Each user has a combobox next to it).
Updated code:
<?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>
Any help on how I can do it?
Screenshot to get a basic idea of what I'm doing:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该将连接代码移动到文档的最顶部:
接下来,您必须将表格包装在标记内:
之后,您将存储
employee_id
或emp_number
(取决于您将使用哪个表键来设置权限)表单上的某处:然后,在
target_url.php
上,您必须执行以下操作:就是这样。
First of all, you should move connection code at the very top of your documents:
Next, you have to wrap your table inside tag:
After that, you would store
employee_id
oremp_number
(depending on what table key you will use for setting permission) somewhere on your form:Then, on your
target_url.php
, you will have to do:That's it.
我建议:
一次你将学习这个,下一次 - 使用。代码将开始组织...
对于初学者来说可能有点复杂。但这是正确的方法。
i suggest:
1 time your will learn this, next - using. Code will start be organized ...
Maybe to complex for starter. But this is right Way.