Home > Backend Development > PHP Problem > An example explains how to implement the single-row save and modification function in a PHP table

An example explains how to implement the single-row save and modification function in a PHP table

PHPz
Release: 2023-04-04 13:22:01
Original
699 people have browsed it

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.

  1. How to generate a table

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>
Copy after login

In the above code, we use a for loop to traverse the data taken from the database, and then display it in the table.

  1. How to add modify and save functions 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=&#39;button&#39; value=&#39;修改&#39;></td>";
            echo "</tr>";
        }
    ?>
</table>
Copy after login

In the above code, we added a modify button to each row.

  1. How to implement the modification operation

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=&#39;post&#39; action=&#39;save.php&#39;>
    <label>姓名:<input type=&#39;text&#39; name=&#39;name&#39; value=&#39;<?php echo $data[0][&#39;name&#39;] ?>'></label>
    <label>性别:<input type=&#39;text&#39; name=&#39;sex&#39; value=&#39;<?php echo $data[0][&#39;sex&#39;] ?>'></label>
    <label>年龄:<input type=&#39;text&#39; name=&#39;age&#39; value=&#39;<?php echo $data[0][&#39;age&#39;] ?>'></label>
    <label>邮箱:<input type=&#39;text&#39; name=&#39;email&#39; value=&#39;<?php echo $data[0][&#39;email&#39;] ?>'></label>
    <input type=&#39;hidden&#39; name=&#39;id&#39; value=&#39;<?php echo $data[0][&#39;id&#39;] ?>'>
    <input type=&#39;submit&#39; value=&#39;保存&#39;>
</form>
Copy after login

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>
Copy after login

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);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template