获取 ComboBox 变量存储在 MySQL 中

发布于 2024-12-06 15:16:49 字数 3474 浏览 0 评论 0原文

我有一个 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;">
    &nbsp;</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;">
    &nbsp;</p>
<p style="text-align: center;">
    &nbsp;</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:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多孤肩上扛 2024-12-13 15:16:49

首先,您应该将连接代码移动到文档的最顶部:

$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_idemp_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 );
        }
    }
}
?>

就是这样。

First of all, you should move connection code at the very top of your documents:

$connection = mysql_connect('localhost','admin','root'); 
if (!$connection)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('users', $connection);

Next, you have to wrap your table inside tag:

<form method="post" action="target_url.php>
    <table>
    ...
    </table>
    <input type="submit" name="submit" value="Save"/>
</form>

After that, you would store employee_id or emp_number (depending on what table key you will use for setting permission) somewhere on your form:

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
}

Then, on your target_url.php, you will have to do:

If target_url.php is the same as your form, then code below should be placed at the very top of your document.

<?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 );
        }
    }
}
?>

That's it.

青春如此纠结 2024-12-13 15:16:49

我建议:

  1. 使用 require_once( "db_connect.php" ) <- 在此文件中建立连接
  2. 使用 smarty & html_options 显示此下拉列表。

一次你将学习这个,下一次 - 使用。代码将开始组织...
对于初学者来说可能有点复杂。但这是正确的方法。

i suggest:

  1. Use require_once( "db_connect.php" ) <- in this file make connection
  2. Use smarty & html_options to show this drop down.

1 time your will learn this, next - using. Code will start be organized ...
Maybe to complex for starter. But this is right Way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文