2012-05-14 48 views
0

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

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

如果我点击A行的编辑按钮,它将允许我编辑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> 
+1

你怎么想编辑?通过表单提交... JavaScript在页面上编辑?这不清楚。 – codemonkee

+0

通过表单提交 – ayou

回答

1

这是可能让你开始的部分答案。 当用户点击该按钮时,您需要执行一个SQL语句是这样的:。

//定义更新查询
$的SQLQuery = “ 更新YourTableName
集列1 ='” $ phpVariable1“ '
列2 =' “$ phpVariable2。” '
其中KEYCOL =''“, “$ phpVariableKey。”;

//运行更新查询
$结果= mysql_query($的SQLQuery);

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

0

从理论上讲,你想给每个行的按钮与在数据库行的唯一值(即button_1,button_2等)。然后,您可以接受并根据提交按钮的值处理表单输入。例如,您可以基于按钮值显示编辑屏幕,或者显示可编辑的文本字段而不是表格中该行的内容。

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