获取特定用户ID后无法更新用户信息

时间:2015-07-13 19:24:14

标签: php mysql mysqli prepared-statement

我有一个名为manageusers的页面,其中列出了我的'用户'中的所有用户。 D b。然后我有一个说编辑的链接。如果点击,它会获得该用户的ID,然后我在名为edituser.php的页面的输入字段中输出他们的信息。

我在下面有两个准备好的陈述。获取用户信息的信息和我正在尝试构建信息的信息,以便将新信息插入/更新到用户记录中的用户记录中。 db table。

现在点击提交后,我收到以下错误..

发生错误 警告:无法修改标头信息 - 已经发送的标头(输出从/home4/db/public_html/example.com/classes/User.php:49开始)

这是我的用户类的代码..

if(!$id && $this->isLoggedIn()) {
        $id = $this->data()->id;
    }
    //echo $this->_db->update('users', $id, $fields);

    //if(!$this->_db->update('users', $id, $fields)) {
        //throw new Exception('There was a problem updating!');
    //}
    try {
        if(!$this->_db->update('users', $id, $fields)) {
            throw new Exception('There was a problem updating!');
        }
    }
       catch(Exception $e) {
    echo "An error occurred";

我正在尝试使用的代码。

 if(isset($_POST['submit'])){
    $firstname = Input::get('firstname');
    $lastname = Input::get('lastname');
    $email = Input::get('email');
    $username = Input::get('username');
    $group = 1;


    $con = mysqli_connect("localhost","root","","db");
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt2 = $con->prepare("INSERT INTO users (firstname, lastname, email, username, `group`) VALUES (?, ?, ?, ?, ?");
    if ( false===$stmt2 ) {
    // Check Errors for prepare
        die('User Request prepare() failed: ' . htmlspecialchars($con->error));
    }
    $stmt2->bind_param('ssssi', $firstname, $lastname, $email, $username, $group);
    if ( false===$stmt2 ) {
    // Check errors for binding parameters
        die('User Request bind_param() failed: ' . htmlspecialchars($stmt2->error));
    }
    $stmt2->execute();
    if ( false===$stmt2 ) {
        die('User Request execute() failed: ' . htmlspecialchars($stmt2->error));
    }


}



//Prepared statement that gets user info    
$con = mysqli_connect("localhost","root","","db");
/* check connection */
   if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt = $con->prepare("SELECT firstname, lastname, email, username, `group` FROM users WHERE id = ?");
    if ( false===$stmt ) {
  // Check Errors for prepare
  die('prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt->bind_param("i", $_GET['id']);
    if ( false===$stmt ) {
      // Check errors for binding parameters
      die('bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
$stmt->execute();
    if ( false===$stmt ) {
      die('execute() failed: ' . htmlspecialchars($stmt->error));
    }
    //Check errors for execute
//if(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);}
$stmt->bind_result($firstname, $lastname, $email, $username, $group);
$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" name="lastname" class="inputbar" 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> 
                        <div class="field">
                            <label for="group">Group</label>
                            <select name="group" required>
                                <option value=''><?php echo htmlentities($group); ?></option>
                                <option value="1">Bench</option>
                                <option value="2">Spectator</option>
                                <option value="3">Team Member</option>
                                <option value="4">Commissioner</option>
                                <option value="5">Creator</option>
                            </select>
                        </div>

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

     User <?php echo htmlentities($_GET['id']); ?> not found.
<? 
} ?>

为什么我会收到此错误以及为什么我无法将新数据更新/插入到我的用户表中?

除了标题消息之外,我从我的用户类收到错误消息。我不相信标题错误消息是其中的主要部分。

0 个答案:

没有答案