逐行编辑

时间:2012-05-14 03:25:18

标签: php mysql html-table edit

我有一个表,其中包含从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>

2 个答案:

答案 0 :(得分:1)

这是部分答案,可能会帮助您入门。 当用户单击该按钮时,您需要执行如下所示的SQL语句:

//定义更新查询
    $ SQLQuery =“     更新YourTableName
    set column1 ='“。$ phpVariable1。” ”,
        column2 ='“。$ phpVariable2。” “
    其中keyCol ='“。$ phpVariableKey。” ';;
//运行更新查询
$ result = mysql_query($ SQLQuery);

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

答案 1 :(得分:0)

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

或者,您可以通过JavaScript将单击处理程序附加到按钮,该代码处理程序将表格单元格的内容替换为特定行的可编辑文本input字段。处理表单的POST时,您可以根据这些新文本字段的值更新数据库。