简体   繁体   中英

Edit a table by row

I have a table which contains data retrieved from a SQL table. 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. 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:

//define the update query
$SQLQuery = " update YourTableName
set column1 = ' ".$phpVariable1." ',
column2 = ' ".$phpVariable2." '
where keyCol = ' ".$phpVariableKey." ' ";

// run the update query
$result = mysql_query($SQLQuery);

$phpVariableKey contains, in your case, the value '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.). 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. When processing the form's POST, you could update the database based on the values of these new text fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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