Home >Backend Development >PHP Problem >An example explains how to implement the single-row save and modification function in a PHP table
With the increasing development of network technology, web applications are becoming more and more popular. Many webmasters write their own websites in PHP language. In fact, the table function in the website is what we need in our daily life, and the table function Modify and save functions are also essential. Today we will talk about how to implement the single-row save and modification function in PHP tables.
First, we need to have a table, which can fetch data from the database and then generate it. In php, we can use for loop to generate tables.
<table> <tr> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>邮箱</th> </tr> <?php for ($i=0; $i<count($data); $i++) { echo "<tr>"; echo "<td>" . $data[$i]['name'] . "</td>"; echo "<td>" . $data[$i]['sex'] . "</td>"; echo "<td>" . $data[$i]['age'] . "</td>"; echo "<td>" . $data[$i]['email'] . "</td>"; echo "</tr>"; } ?> </table>
In the above code, we use a for loop to traverse the data taken from the database, and then display it in the table.
In the table, we need to add modify and save buttons for each row. When adding modification buttons, we can add a button to the last column of each row or add a modification button to the last cell of each column of each row. The second method is used below:
<table> <tr> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>邮箱</th> <th>操作</th> </tr> <?php for ($i=0; $i<count($data); $i++) { echo "<tr>"; echo "<td>" . $data[$i]['name'] . "</td>"; echo "<td>" . $data[$i]['sex'] . "</td>"; echo "<td>" . $data[$i]['age'] . "</td>"; echo "<td>" . $data[$i]['email'] . "</td>"; echo "<td><input type='button' value='修改'></td>"; echo "</tr>"; } ?> </table>
In the above code, we added a modify button to each row.
After clicking the modify button, we need to display the data of this row in the form, and then the user can modify the data in the form. After modifying the data, we need to save it back to the database.
First, we need to display the data of the current row in the form.
<form method='post' action='save.php'> <label>姓名:<input type='text' name='name' value='<?php echo $data[0]['name'] ?>'></label> <label>性别:<input type='text' name='sex' value='<?php echo $data[0]['sex'] ?>'></label> <label>年龄:<input type='text' name='age' value='<?php echo $data[0]['age'] ?>'></label> <label>邮箱:<input type='text' name='email' value='<?php echo $data[0]['email'] ?>'></label> <input type='hidden' name='id' value='<?php echo $data[0]['id'] ?>'> <input type='submit' value='保存'> </form>
After the user completes the modification, the new data needs to be saved to the database. Generally, we will use ajax to submit the form data.
<script> $(function(){ $("form").submit(function(){ var data = $(this).serialize(); $.ajax({ type: 'POST', url: 'save.php', data: data, dataType: 'json', success: function(data){ if (data.result == 'success') { alert('保存成功!'); } else { alert('保存失败,请检查!'); } } }); return false; }); }); </script>
In the above code, we use jQuery's ajax method to submit form data.
Finally, we need to save this row of data to the database in save.php.
$id = $_POST['id']; $name = $_POST['name']; $sex = $_POST['sex']; $age = $_POST['age']; $email = $_POST['email']; $sql = "UPDATE `table_name` SET `name`='$name', `sex`='$sex', `age`='$age', `email`='$email' WHERE `id`='$id'"; $result = mysql_query($sql); if ($result) { $data['result'] = 'success'; } else { $data['result'] = 'fail'; } echo json_encode($data);
In save.php, we receive the data submitted by the form, update the modified data to the database, and return a result in json format. At this point, the function of saving modifications in a single row of the PHP form has been implemented.
The above is the detailed content of An example explains how to implement the single-row save and modification function in a PHP table. For more information, please follow other related articles on the PHP Chinese website!