单击编辑按钮时如何传递所有用户的数据?

How to pass all user's data when clicking edit button?

我使用 php 创建了一个显示页面,其中所有用户记录都显示在 table 中,并且有删除和编辑按钮来管理用户,对于删除选项我设法做到了但是自昨天尝试执行编辑按钮以来已经有几个小时了。我想要将所有用户数据(full_name、电子邮件、密码...城市)从 display.php 页面传递到 update.php,并且我想要将更新的新数据通过我制作的表格输入 updateform.html 但我了解了如何连接这 3 个文件以成功更新用户详细信息。

文件如下:

display.php

<!DOCTYPE html>
<html>

<head>
    <style>
        table,
        td,
        th {
            border: 1.5px solid #d9d9d9;
            border-collapse: collapse;
        }

        table {
            width: 100%;
        }
    </style>
</head>

<body>
        </div>

    <div class="container">

        <?php

        $conn = mysqli_connect("localhost", "root", "", "finalproject") or die("<script>alert('Connection Failed.')</script>");
        $sql = "SELECT * FROM users" or die("<script>alert('Connection Failed.')</script>");
        $result = mysqli_query($conn, $sql);
        ?>

        <div class="row justify-contect-center">
            <table class="table" border-size="1">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Full Name</th>
                        <th>Email</th>
                        <th>Password</th>
                        <th>Age</th>
                        <th>Gender</th>
                        <th>Phone Number</th>
                        <th>Work Duration</th>
                        <th>IG Account</th>
                        <th>State</th>
                        <th>Postcode</th>
                        <th>City</th>
                        <th>Action</th>

                    </tr>
                </thead>
                <?php
                //while($row = $result->fetch_assoc()); 
                while ($row = $result->fetch_assoc()) :
                ?>
                    <tr>
                        <td><?php echo $row['username'] ?></td>
                        <td><?php echo $row['full_name'] ?></td>
                        <td><?php echo $row['email'] ?></td>
                        <td><?php echo $row['password'] ?></td>
                        <td><?php echo $row['age'] ?></td>
                        <td><?php echo $row['gender'] ?></td>
                        <td><?php echo $row['phone_number'] ?></td>
                        <td><?php echo $row['work_duration'] ?></td>
                        <td><?php echo $row['ig_account'] ?></td>
                        <td><?php echo $row['state'] ?></td>
                        <td><?php echo $row['postcode'] ?></td>
                        <td><?php echo $row['city'] ?></td>
                        <td>
                            <a href="updateform.html?edit=<?php if(isset($username)){
                                $conn->query("Select * from users where username='$username'");
                            } ?>" class="btn btn-info">Edit</a>

                            <a href="delete.php?delete=<?php echo $row['username']; ?>" class="btn btn-danger">Delete</a>
                        </td>
                    </tr>

                <?php endwhile; ?>
            </table>

        </div>

        <?php
        function pre_r($array)
        {
            echo '<pre>';
            print_r($array);
            echo '</pre>';
        }

        ?>

</body>

</html>

updateform.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

    <link rel="stylesheet" type="text/css" href="style.css">

    <title>Update Information</title>
</head>

<body>
    <div class="container">
        <form action="update.php" method="POST" class="login-email">
            <p class="login-text" style="font-size: 2rem; font-weight: 800;">Update Information</p>
 
            <div class="input-group" style="margin-bottom: 40px;">Full Name:
                <input type="text" name="full_name" value="" required>
            </div>
            <div class="input-group" style="margin-bottom: 40px;">Email:
                <input type="email" name="email" value="" required>
            </div>
            <div class="input-group" style="margin-bottom: 40px;">Password:
                <input type="password" name="password" value="" required>
            </div>
            <div class="input-group" style="margin-bottom: 40px;">Confirm Password:
                <input type="password" name="cpassword" value="" required>
            </div>
            <div class="input-group" style="margin-bottom: 40px;">Age:
                <input type="text" name="age" value="" required>
            </div>
            <div style="margin-top: 40px; margin-bottom: 20px;">Gender:
                <label for="f-option" class="l-radio" value="">
                    <input type="radio" name="gender" tabindex="1" value="Male" style="margin-left:10px;">
                    <span> Male</span>
                    <input type="radio" name="gender" tabindex="2" value="Female" style="margin-left:20px;">
                    <span> Female</span>
            </div>
            <div class="input-group" style="margin-bottom: 40px;">Phone Number:
                <input type="text" name="phone_number" value="" required>
            </div>
            <div class="input-group" style="margin-bottom: 40px;">Work Duration:
                <input type="text" name="work_duration" value="" required>
            </div>
            <div class="input-group" style="margin-bottom: 40px;">IG Account:
                <input type="text" name="ig_account" value="" required>
            </div>
            <div class="form-group" style="margin-bottom: 10px;">
                <label>State:</label>
                <select name="state" class="form-control" value="" required>
                    <option value="1">Option 1</option>
                    <option value="2">Option 2</option>
                    <option value="3">Option 3</option>
                </select>
            </div>
            <div class="input-group" style="margin-bottom: 40px;">Postcode:
                <input type="text" name="postcode" value="" required>
            </div>
            <div class="input-group" style="margin-bottom: 50px;">City:
                <input type="text" name="city" value="" required>
            </div>
            <div class="input-group" style="margin-top:40px;">
                <button name="update" class="btn">Update</button>
            </div>
        </form>
    </div>

</body>

</html>

update.php

<?php

$conn = mysqli_connect("localhost", "root", "", "finalproject") or die("<script>alert('Connection Failed.')</script>");

if (isset($_POST['update'])) {
    $full_name = $_POST['full_name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['cpassword'];
    $phone_number = $_POST['phone_number'];
    $work_duration = $_POST['work_duration'];
    $ig_account = $_POST['ig_account'];
    $state = $_POST["state"];
    $postcode = $_POST['postcode'];
    $city = $_POST['city'];
    $gender = $_POST['gender'];
    $age = $_POST['age'];

}
?>

我发现你的代码有很多问题,所以我竭尽全力为你重新编码,甚至为你做了更新部分,希望你能从中学到一些技巧。根据你的代码,我假设你对游戏还很陌生......我添加了额外的文件,让你的生活更轻松 运行。

*一些小技巧

  • 您不需要在文件末尾使用 ?> 关闭 php
  • <?=$username?> 等同于 <? echo $username; ?>。简单明了。
  • 尽量不要使用表格,我并没有把你弄乱,但请查看表格的 flex 或使用 div
  • 不要在 html 表中使用 border-size...尽量不要在 html 中的任何内容中使用样式...始终使用 css。 类 被多次使用 .classname 和 ID 仅被使用一次 #idname*

_config.php 将您的数据库和其他设置放入此文件以包含到您的所有页面中,这样您就不会浪费时间一遍又一遍地写同样的东西

<?php
$db = new mysqli('localhost', 'user', 'pass', 'riseofwar_v1');
if ($db->connect_errno > 0) {
    die('Unable to connect to database [' . $db->connect_error . ']');
}

_funcs.php 我给你我的基本库来清理你的输入字段

<?php
include('_config.php');

// BASIC FUNCTIONS; Carry this library to all your future projects too!
function abc ($input){ return preg_match('/^[A-Z]+$/i', $input); }
function abcSpc ($input){ return preg_match('/^[a-z][a-z\ ]*$/i', $input); }
function abcNum ($input){ return preg_match('/^[A-Z0-9]+$/i', $input); }
function abcNumSpc ($input){ return preg_match('/^[A-Z0-9\ ]+$/i', $input); }
function abcNumU ($input){ return preg_match('/^[A-Z0-9_-]+$/i', $input); }
function abcNumD ($input){ return preg_match('/^[A-Z0-9-]+$/i', $input); }
function num ($input){ if(strlen($input) > 24){ $input=0; }if(!preg_match('/^[0-9]+$/', $input)){ $input=0; } return $input; }
function numDot ($input){ if(strlen($input) > 24){ $input=0; }if(!preg_match('/^[0-9.]+$/', $input)){ $input=0; } return $input; }
function numU ($input){ return preg_match('/^[0-9_-]+$/i', $input); }
function is_odd($num){ return($num & 1); }
function email ($input){ return filter_var($input, FILTER_VALIDATE_EMAIL); }
function phone ($input){ $phone = preg_replace('/[^0-9]/', '', $input); if(strlen($phone) < 10 || !num($phone)) { $input=false; } return $input; }
function is_url($input){ return preg_match('/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i',$input); }
function is_uri ($input){ return preg_match('/^[a-z0-9-]+$/i', $input); }
function findIt($find,$string){ return preg_match("/$find/i","$string"); }

_html.php 你会喜欢这个...修改你创建的每个 html 页面的页眉和页脚...这是文件您包括在所有现有页面和新页面中。

<?php
include('_funcs.php');

// Simple function you can call on every page so you don't have to edit each file every time you change something in the headers
function head($title){?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" type="text/css" href="style.css">
        <title><?=$title?></title>
    </head>
    <body>
<?
}

// Same as the above function, just the footer
function foot(){?>
    </body>
    </html>
<? }

display.php

<?php
include('_html.php');

head('Viewing Users');
?>
<div class="container">

    <div class="row justify-contect-center">
        <table class="table" border-size="1">
            <thead>
            <tr>
                <th>Username</th>
                <th>Full Name</th>
                <th>Email</th>
                <th>Password</th>
                <th>Age</th>
                <th>Gender</th>
                <th>Phone Number</th>
                <th>Work Duration</th>
                <th>IG Account</th>
                <th>State</th>
                <th>Postcode</th>
                <th>City</th>
                <th>Action</th>

            </tr>
            </thead>
            <?php
            $result = $db->query("SELECT * FROM users ORDER BY id LIMIT 25;");
                while ($row = $result->fetch_assoc()) {?>
                <tr>
                    <td><?php echo $row['username'] ?></td>
                    <td><?php echo $row['full_name'] ?></td>
                    <td><?php echo $row['email'] ?></td>
                    <td><?php echo $row['password'] ?></td>
                    <td><?php echo $row['age'] ?></td>
                    <td><?php echo $row['gender'] ?></td>
                    <td><?php echo $row['phone_number'] ?></td>
                    <td><?php echo $row['work_duration'] ?></td>
                    <td><?php echo $row['ig_account'] ?></td>
                    <td><?php echo $row['state'] ?></td>
                    <td><?php echo $row['postcode'] ?></td>
                    <td><?php echo $row['city'] ?></td>
                    <td>
                        <a href="edit.php?id=<?=$row['id']?>" class="btn btn-info">Edit</a>

                        <a href="delete.php?delete=<?php echo $row['username']; ?>" class="btn btn-danger">Delete</a>
                    </td>
                </tr>

            <?}?>
        </table>

    </div>
<? foot();

edit.php

<?php
include('_html.php');

$id = num($_GET['id']) ?: false; // Check the ID in the URL to make sure it's a number

$user = $db->query("SELECT * FROM users WHERE id='$id'")->fetch_assoc();

if (isset($_POST['update'])) {

    // CHECK VALUES BEFORE YOU UPDATE THEM
    $user['full_name'] = abcSpc($_POST['full_name']) ? $_POST['full_name'] : $user['full_name'];
    $user['email'] = email($_POST['email'])  ? $_POST['email'] : $user['email'];
    $user['password'] = abcNum($_POST['password']) ? $_POST['password'] : $user['password'];
    $user['password'] = $user['password'] != $user['cpassword'] ? $user['password'] : $_POST['password'];
    $user['phone_number'] = phone($_POST['phone_number']) ? $_POST['phone_number'] : $user['phone_number'];
    $user['work_duration'] = num($_POST['work_duration']) ? $_POST['work_duration'] : $user['work_duration'];
    $user['ig_account'] = abcSpc($_POST['ig_account']) ? $_POST['ig_account'] : $user['ig_account'];
    $user['state'] = num($_POST["state"]) ? $_POST['state'] : $user['state'];
    $user['postcode'] = abc($_POST['postcode']) ? $_POST['postcode'] : $user['postcode'];
    $user['city'] = num($_POST['city']) ? $_POST['city'] : $user['city'];
    $user['gender'] = num($_POST['gender'])  ? $_POST['gender'] : $user['gender'];
    $user['age'] = num($_POST['age']) ? $_POST['age'] : $user['age'];

    $msg = 'Updated! <a href="display.php">View all users again</a>';

    $db->query("UPDATE users SET full_name='$user[full_name]', email='$user[email]', password='$user[password]', phone_number='$user[phone_number]', work_duration='$user[work_duration]', ig_account='$user[ig_account]', state='$user[state]', postcode='$user[postcode]', city='$user[city]', gender='$user[gender]', age='$user[age]' WHERE id='$user[id]';");
}




head('Update Information');
?>
<?=$msg?>
<div class="container">
    <form method="POST" class="login-email">
        <p class="login-text" style="font-size: 2rem; font-weight: 800;">Update Information</p>

        <div class="input-group" style="margin-bottom: 40px;">Full Name:
            <input type="text" name="full_name" value="<?=$user['full_name']?>" required>
        </div>
        <div class="input-group" style="margin-bottom: 40px;">Email:
            <input type="email" name="email" value="<?=$user['email']?>" required>
        </div>
        <div class="input-group" style="margin-bottom: 40px;">Password:
            <input type="password" name="password" value="">
        </div>
        <div class="input-group" style="margin-bottom: 40px;">Confirm Password:
            <input type="password" name="cpassword" value="">
        </div>
        <div class="input-group" style="margin-bottom: 40px;">Age:
            <input type="text" name="age" value="<?=$user['age']?>" required>
        </div>
        <div style="margin-top: 40px; margin-bottom: 20px;">Gender:
            <label for="f-option" class="l-radio" value="">
                <input type="radio" name="gender" checked tabindex="1" value="Male" style="margin-left:10px;">
                <span> Male</span>
                <input type="radio" name="gender" tabindex="2" value="Female" style="margin-left:20px;">
                <span> Female</span>
        </div>
        <div class="input-group" style="margin-bottom: 40px;">Phone Number:
            <input type="text" name="phone_number" value="<?=$user['phone_number']?>" required>
        </div>
        <div class="input-group" style="margin-bottom: 40px;">Work Duration:
            <input type="text" name="work_duration" value="<?=$user['work_duration']?>" required>
        </div>
        <div class="input-group" style="margin-bottom: 40px;">IG Account:
            <input type="text" name="ig_account" value="<?=$user['ig_account']?>" required>
        </div>
        <div class="form-group" style="margin-bottom: 10px;">
            <label>State:</label>
            <select name="state" class="form-control" required>
                <option value="1" <?=$state==1?'checked':''?>>Option 1</option>
                <option value="2" <?=$state==2?'checked':''?>>Option 2</option>
                <option value="3" <?=$state==3?'checked':''?>>Option 3</option>
            </select>
        </div>
        <div class="input-group" style="margin-bottom: 40px;">Postcode:
            <input type="text" name="postcode" value="<?=$user['postcode']?>" required>
        </div>
        <div class="input-group" style="margin-bottom: 50px;">City:
            <input type="text" name="city" value="<?=$user['city']?>" required>
        </div>
        <div class="input-group" style="margin-top:40px;">
            <button name="update" class="btn">Update</button>
        </div>
    </form>
</div>
<? foot();