将用户的信息传递到新页面然后进行编辑

时间:2015-06-17 18:51:20

标签: php html forms

我将所有用户输出到我称为admincustomers.php的页面上的表格中。然后我尝试在特定用户记录上选择EDIT。然后我将其路由到名为editusers.php的页面。然后,我希望在该页面上输出所有特定用户信息。我没有在admincustomers.php上显示我的表中的所有用户信息。因此,当编辑页面加载时,我希望获得所有用户信息,以便我能够编辑它。

我遇到的问题是我不太确定如何继承客户ID并将信息提供给下一页。

admincustomers.php页面

$con = mysqli_connect("localhost","root","","bfb");
    $q = mysqli_query($con,"SELECT * FROM users");
?>                  
                            <table class="tableproduct">
                                <tr>
                                    <th class="thproduct">ID</th>
                                    <th class="thproduct">First Name</th>
                                    <th class="thproduct">Last Name</th>
                                    <th class="thproduct">Email</th>
                                    <th class="thproduct">Username</th>
                                    <th class="thproduct">Group</th>
                                    <th class="thproduct"></th>
                                    <th class="thproduct"></th>
                                </tr>   
<?php   
     if(isset($_POST['id']) && is_numeric($_POST['id'])) {
       mysqli_query($con, "DELETE FROM users WHERE id = ". $_POST['id'] ."")
       or die("Could not DELETE: " . mysqli_error($con));
         "Your product was successfully deleted."; 
}   else {Session::flash('adminusers', 'User was successfully deleted.');
}   
        while($row = mysqli_fetch_assoc($q)) : 
?>
                            <form method="POST" action="admincustomers.php">
                                <tr>
                                    <td class="tdproduct"><?php echo $row['id']; ?> </td>
                                    <td class="tdproduct"><?php echo $row['firstname']; ?> </td> 
                                    <td class="tdproduct"><?php echo $row['lastname']; ?> </td>
                                    <td class="tdproduct"><?php echo $row['email']; ?> </td>
                                    <td class="tdproduct"><?php echo $row['username']; ?> </td>
                                    <td class="tdproduct"><?php echo $row['group']; ?> </td>
                                    <td class="tdproduct"><a href='edituser.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>
                                    <input type="hidden" name="id" value="<? echo $row['id']; ?>"/>
                                    <td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
                                </tr>
                            </form> 

<?php   
endwhile;
//Delete message
    if(Session::exists('adminusers')) {
        echo '<p>' . Session::flash('adminusers') . '</p>';
    }
?>
                        </table>    

editusers.php页面

我试图逃避用户数据,但我只是将我的用户信息转义到页面上。

<?php
    $_GET['id'];
?>
                    <form action="" method="post">
                            <div class="field">
                                <label for="firstname">First name</label>
                                <input type="text" name="firstname" class="inputbar" value="<?php echo escape($user->data()->firstname); ?>" required>
                            </div>
                            <div class="field">
                                <label for="lastname">Last name</label>
                                <input type="text" class="inputbar" name="lastname" value="<?php echo escape($user->data()->lastname); ?>" required>
                            </div>
                            <div class="field">
                                <label for="email">Email</label>
                                <input type="email" class="inputbaremail" name="email" value="<?php echo escape($user->data()->email); ?>" required>
                            </div>
                            <div class="field">
                                <label for="username">Username</label>
                                <input type="text" class="inputbar" name="username" value="<?php echo escape($user->data()->username); ?>" required>
                            </div>  

                                <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
                                <label for="signinButton">
                                    <input type="submit" id="signinButton" value="Update">
                                </label>
                        </form>

我做错了什么,如何解决这个问题?

更新:

$ user variable

public function __construct($user = null) {
    $this->_db = DB::getInstance();

    $this->_sessionName = Config::get('session/session_name');
    $this->_cookieName = Config::get('remember/cookie_name');

    if(!$user) {
        if(Session::exists($this->_sessionName)) {
            $user = Session::get($this->_sessionName);

            if($this->find($user)) {
                $this->_isLoggedIn = true;
            } else {
                // process Logout
            }
        }
    } else {
        $this->find($user);
    }
}

1 个答案:

答案 0 :(得分:1)

editusers.php脚本中,在$_GET['id']中查找用户并使用该信息,而不是$user

<?php
$con = mysqli_connect("localhost","root","","bfb");
$stmt = $con->prepare("SELECT firstname, lastname, email, username FROM users WHERE id = ?");
$stmt->bind_param("i", $_GET['id']);
$stmt->execute();
$stmt->bind_result($firstname, $lastname, $email, $username);
$stmt->store_result();
if ($stmt->fetch()) { ?>
    <form action="" method="post">
        <div class="field">
            <label for="firstname">First name</label>
            <input type="text" name="firstname" class="inputbar" value="<?php echo htmlentities($firstname); ?>" required>
        </div>
        <div class="field">
            <label for="lastname">Last name</label>
            <input type="text" class="inputbar" name="lastname" value="<?php echo htmlentities($lastname); ?>" required>
        </div>
        <div class="field">
            <label for="email">Email</label>
            <input type="email" class="inputbaremail" name="email" value="<?php echo htmlentities($email); ?>" required>
        </div>
        <div class="field">
            <label for="username">Username</label>
            <input type="text" class="inputbar" name="username" value="<?php echo htmlentities($username); ?>" required>
        </div>  

            <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
            <label for="signinButton">
                <input type="submit" id="signinButton" value="Update">
            </label>
    </form>
<?php } else { ?>
    <div>
    User <?php echo htmlentities($_GET['id']); ?> not found.
<? 
}