链接HTML表与MySQL表

链接HTML表与MySQL表

本文介绍了链接HTML表与MySQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格显示来自MySQL表格的数据。我有一个额外的列,为每个条目添加一个复选框。

是否可以将该列行与包含MySQL数据的列中的其他数据关联?

p>

然后按下'Save'按钮将复选框条目保存到新的mysql表中。



这里是一张照片向你展示我的意思:



代码:

 <?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} \\\
;
mysql_query($ sql);
}
}
}
?>
< p style =text-align:center;>
< 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;
颜色:白色;
font-style:Calibri;
}
< / style>
< / head>

< form method =postaction =admin.php>



if(!$ connection)
{
die('Could not connect:'。mysql_error( ));
}

mysql_select_db('users',$ connection);
$ b // 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);
$ b $ mysql_query(INSERT INTO权限(emp_number,employee_id,emp_firstname,emp_lastname)
SELECT emp_number,employee_id,emp_firstname,emp_lastname $ b $ FROM hs_hr_employee
DUPLICATE KEY UPDATE employee_id = VALUES(employee_id),emp_number = VALUES(emp_number)
); $ *
$ b $ 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);
$ b $ 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>
Employee Number< / th>
< th> ID< / th>
第r个姓名第
个姓氏第$个$ $第b个权限第$ $ $ $第b个变更< / th>
< th> th>
< / tr>;

while($ row = mysql_fetch_array($ result))
{
echo< tr>;
回显< td> 。 $ row ['emp_number']。 < / TD> 中;
回显< td> 。 $ row ['employee_id']。 < / TD> 中;
回显< td> 。 $ row ['emp_firstname']。 < / TD> 中;
回显< td> 。 $ row ['emp_lastname']。 < / TD> 中;
回显< td> 。 $ row ['Permissions']。 < / TD> 中;
echo< td>< select name ='cb_change []'>< option value ='all'>全部< / option>< option value ='remote'>远程游戏< / option>< option value ='landbased'> Landbased Gaming< / option>< option value ='general'> General Gaming< / option>< / select>< / td>;
回声< / 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_Btntype =buttonvalue =Save/>


< / p>


< / form>

任何帮助都会得到满足。

解决方案

假设您的意思是选择列表标题栏中的另一列,名称为更改。
在变更后添加另一个表格单元格...

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

发布表单后,$ _POST ['fieldName']数组的ID将为

尝试并确保$ row ['emp_number']来自您的表主键,以确保该值是唯一的。

p>

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.

解决方案

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.

这篇关于链接HTML表与MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 01:26