Linking HTML table with MySQL table

百般思念 提交于 2019-12-12 03:01:10

问题


I have a table that displays data from a MySQL table. I have an extra column that adds a checkbox for each entry.

Is it possible to link that column row with the other data in the columns containing the MySQL data?

The checkbox entries will then be saved to a new mysql table upon pressing the 'Save' button.

Here is a picture to show you what I mean:

Code:

<?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)
        {
            $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;">
    &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);

        //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>";

        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[]'><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 "<td>" . $row['Change'] . "</td>";


          echo "</table>";

          echo "</center>";


        #$_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 would be kindly appreciated.


回答1:


Assuming you mean another column after the select list column titled 'change'. Add another table cell after the change colum with this...

echo "<td> <input type="checkbox" name='fieldName[]' value="<?php echo $row['emp_number']; ?>" > </td>";

After you've posted the form the $_POST['fieldName'] array will have the ids for the rows that have been clicked.

Try and make sure the $row['emp_number'] is from your tables primary key to make sure the value is unique.



来源:https://stackoverflow.com/questions/7566401/linking-html-table-with-mysql-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!