繁体   English   中英

逐行编辑表格

[英]Edit a table by row

我有一个表,其中包含从SQL表检索的数据。 我在每一行的末尾都附加了一个编辑按钮。 编辑按钮允许用户编辑有关特定项目的信息。 我希望编辑按钮允许用户将其附加到的信息。 例如:

--------------------------
NAME | AGE | GENDER |
--------------------------
  A  |  4  | Female |EDIT
--------------------------
  B  |  9  | Female |EDIT
--------------------------
  C  |  2  |  Male  |EDIT
--------------------------

如果单击A的行上的EDIT按钮,则可以编辑A的信息。 请帮忙。

<form id="edit" name="edit" method="post" action="edititem.php">
    <table width="792" border='1' align='center' cellpadding='0' cellspacing='0' id='usertable'>
    <tr bgcolor=#706C4B  style='color:black'>
        <td width="16%" align='center'  id="box_header2" style='width:10%'>Item Name</td>
        <td width="16%" align='center'  id="box_header2" style='width:10%'>Item Quantity</td>
        <td width="14%" align='center' id="box_header2" style='width:13%'>Storage Capacity</td>
        <td width="13%" align='center' id="box_header2" style='width:11%'>Brand</td>
        <td width="13%" align='center' id="box_header2" style='width:11%'>Color</td>
        <td width="13%" align='center' id="box_header2" style='width:12%'>MAC Address</td>
        <td width="13%" align='center' id="box_header2" style='width:12%'>S/N Number</td>
        <td width="13%" align='center' id="box_header2" style='width:12%'></td>
    </tr>

 <?php
 $sql="select * from item";
 $result=mysql_query($sql);
  while ($row=mysql_fetch_array($result))
    {
    ?>
    <tr bgcolor=#cfccb7 style='color:black'>  
        <td><div align="center"><?php echo $row['item_name']?></div></td>
        <td><div align="center"><?php echo $row['item_quantity']?></div></td>
        <td><div align="center"><?php echo $row['item_capacity']?></div></td>
        <td><div align="center"><?php echo $row['item_brand']?></div></td>
        <td><div align="center"><?php echo $row['item_color']?></div></td>
        <td><div align="center"><?php echo $row['item_mac']?></div></td>
        <td><div align="center"><?php echo $row['item_sn']?></div></td>
        <td><div align="center"><input type="submit" name="button" id="button" value="Edit" /></div></td>
    </tr>
     <?php
     }
     ?>


    </table>
    </form>

这是部分答案,可能会让您入门。 当用户单击按钮时,您需要执行以下SQL语句:

//定义更新查询
$ SQLQuery =“更新YourTableName
设置column1 ='“。$ phpVariable1”。 ',
column2 ='“。$ phpVariable2。” '
其中keyCol ='“。$ phpVariableKey。” '“;

//运行更新查询
$ result = mysql_query($ SQLQuery);

在您的情况下,$ phpVariableKey包含值“ A”。

从理论上讲,您希望为每一行的按钮赋予与数据库中该行相关的唯一值(即button_1,button_2等)。 然后,您可以基于“提交”按钮的值来接受并处理表单输入。 例如,您可以显示基于按钮值的编辑屏幕,或显示可编辑的文本字段,而不是表格中该行的内容。

另外,您可以通过JavaScript将点击处理程序附加到按钮上,从而用特定行的可编辑文本input字段替换表格单元格的内容。 处理表单的POST时,您可以根据这些新文本字段的值来更新数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM