从管理员帐户编辑用户

时间:2015-07-06 22:32:40

标签: php mysql

我一直在玩编辑用户个人资料(登录为),这个工作正常。

所以我决定尝试编辑其他用户配置文件,方法是用一个表填充用户(工作正常,并且禁止和删除工作正常),单击带有id的超链接(工作正常),显示用户名配置文件及其当前配置文件信息(工作正常)和要更新的表单与编辑会话用户表单相同(也可以正常工作)。

但是在提交时,它不会更新记录。

我正在使用$userID = $_GET['id'];并将其绑定到sql select id命令。

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

include_once '../includes/conn.php';

if(!$user->is_loggedin()){
    $user->redirect('../users/login.php');
}

$userID = $_GET['id'];
$stmt = $conn->prepare("SELECT * FROM users WHERE id=:id");
$stmt->execute(array(":id"=>$userID));

$userRow=$stmt->fetch(PDO::FETCH_ASSOC);

if (isset($_POST['update'])) {
    $location = isset($_POST['location']) ? $_POST['location'] : null;
    $about = isset($_POST['about']) ? $_POST['about'] : null;
    $title = isset($_POST['title']) ? $_POST['title'] : null;

    $sql_part = array();
    $prepare = array();
    if ($location) {
        $sql_part[] = 'location = :location';
        $prepare[':location'] = $location;
    }
    if ($about) {
        $sql_part[] = 'about = :about';
        $prepare[':about'] = $about;
    }
    if ($title) {
        $sql_part[] = 'title = :title';
        $prepare[':title'] = $title;
    }
    $prepare[':id'] = $userID;

    if (count($sql_part)) {
        $sql = 'UPDATE users SET ';
        $sql .= implode(', ', $sql_part);
        $sql .= ' WHERE id = :id';

        $stmt = $conn->prepare($sql);

        if ($stmt) {
            $result = $stmt->execute($prepare);
            $count = $stmt->rowCount();
            header('Location: '. $_SERVER['REQUEST_URI']);
            exit;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">    
<head>
    <title>EpicOwl UK | CMS Users Edit Profile</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body>
<div id="header">
    <a href="index.php"><img id="logo" src="../images/logo.png" /></a>
    <div id="navigation">
        <ul>
            <a href="../index.php"><li>Home</li></a>
            <a href="./profile.php"><li>My Profile</li></a>
            <a href="../admin/index.php"><li>Admin Panel</li></a>
        </ul>
    </div>
</div>
<div id="content">
<form method="post"><br />
    <h2>Edit <?php echo ($userRow['username']); ?>'s Profile</h2>
    <label><strong>User Title:</strong></label><br />
    <input type="text" name="title" maxlength="50" placeholder="<?php echo ($userRow['title']); ?>" /><br /><br />
    <label><strong>My Location:</strong></label><br />
    <input type="text" name="location" maxlength="50" placeholder="<?php echo ($userRow['location']); ?>" /><br /><br />
    <label><strong>About Me:</strong><label><br />
    <textarea name="about" rows="13" cols="60" maxlength="255" placeholder="<?php echo ($userRow['about']); ?>"></textarea><br /><br />
    <button type="submit" name="update">Update</button><br /><br /><br />
</form>
</div>
<div id="footer">
    <p class="copyright">&copy; EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>

0 个答案:

没有答案