0

I have a page called manageusers that lists all of the users in my 'users' db. I then have a link that says edit. If clicked on, it gets that user's id and then I output their information in input fields in a page called edituser.php.

I have two prepared statements below. One that gets the user's information and one that I am trying to structure so that it insert/updates the new information into that users record in the 'users' db table.

Right now after I hit submit, I get the following error..

An error occurred Warning: Cannot modify header information - headers already sent by (output started at /home4/db/public_html/example.com/classes/User.php:49)

This is the code from my user class..

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";

My code that I am trying to do this with.

 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.
<? 
} ?>

Why would I be getting this error and why can't I update/insert the new data into my users table?

I am getting an error message from my user class besides the header message. I do not believe the header error message is the main part to this.

Paul
  • 3,348
  • 5
  • 32
  • 76
  • What is in the line 49? You are not allowed to output anything before you use header() method. – jedrzej.kurylo Jul 13 '15 at 19:29
  • I never tried to use the header() method. Line 49 of the user class is `echo "An error occurred";` – Paul Jul 13 '15 at 19:34
  • Sure, but when you use sessions PHP will try to send headers. Do not echo anything or, best, fix the query that fails and results in exception being thrown. – jedrzej.kurylo Jul 13 '15 at 19:41
  • I don't know what is wrong with my query or code for this matter for this to happen? – Paul Jul 13 '15 at 19:56

0 Answers0