开发者

Getting ComboBox variable to store in MySQL

开发者 https://www.devze.com 2023-04-07 20:14 出处:网络
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_employ

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;">
    &nbsp;</p>

<head>
<style type="text/css">
table, td, th
{
border:1px solid #666;
font-style:Calibri;
}
th
{
开发者_JS百科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>

Any help on how I can do it?

Screenshot to get a basic idea of what I'm doing:

Getting ComboBox variable to store in MySQL


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消