简体   繁体   English

逐行编辑表格

[英]Edit a table by row

I have a table which contains data retrieved from a SQL table. 我有一个表,其中包含从SQL表检索的数据。 I've attached an edit button at the end of every row. 我在每一行的末尾都附加了一个编辑按钮。 the edit button allows the user to edit information on a certain item. 编辑按钮允许用户编辑有关特定项目的信息。 I want the edit button to allow the user the information it is attached to. 我希望编辑按钮允许用户将其附加到的信息。 For example: 例如:

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

If I click the EDIT button on A's row, it would allow me to edit A's information. 如果单击A的行上的EDIT按钮,则可以编辑A的信息。 Please help. 请帮忙。

<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>

This is a partial answer that may get you started. 这是部分答案,可能会让您入门。 When the user clicks the button, you need to execute a SQL statement something like this: 当用户单击按钮时,您需要执行以下SQL语句:

//define the update query //定义更新查询
$SQLQuery = " update YourTableName $ SQLQuery =“更新YourTableName
set column1 = ' ".$phpVariable1." 设置column1 ='“。$ phpVariable1”。 ', ',
column2 = ' ".$phpVariable2." column2 ='“。$ phpVariable2。” ' '
where keyCol = ' ".$phpVariableKey." 其中keyCol ='“。$ phpVariableKey。” ' "; '“;

// run the update query //运行更新查询
$result = mysql_query($SQLQuery); $ result = mysql_query($ SQLQuery);

$phpVariableKey contains, in your case, the value 'A'. 在您的情况下,$ phpVariableKey包含值“ A”。

In theory, you would want to give each row's button a unique value related to that row in the database (ie button_1, button_2, etc.). 从理论上讲,您希望为每一行的按钮赋予与数据库中该行相关的唯一值(即button_1,button_2等)。 Then, you could accept and process the form input based on the submit button's value. 然后,您可以基于“提交”按钮的值来接受并处理表单输入。 For example, you could display an edit screen based on the button value, or display editable text fields instead of the content for that row in the table. 例如,您可以显示基于按钮值的编辑屏幕,或显示可编辑的文本字段,而不是表格中该行的内容。

Alternatively, you could attach a click handler to the button via JavaScript that replaces the table cell's content with editable text input fields for a particular row. 另外,您可以通过JavaScript将点击处理程序附加到按钮上,从而用特定行的可编辑文本input字段替换表格单元格的内容。 When processing the form's POST, you could update the database based on the values of these new text fields. 处理表单的POST时,您可以根据这些新文本字段的值来更新数据库。

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

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