Location: PHPKode > scripts > PHP Address book > php-address-book_v_1.02_eng/user_edit.php
<?php 
include_once 'includes/header.inc.php';

if(!$session->getRole() == 2){
    header("location: login.php");
}

$id = $_GET['id'];

//when posted
if(isset($_POST['submit'])){
    $role = $_POST['role'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
        
    if($_POST['mailinglist'] == 'on'){
        $mailinglist = 1;
    } else {
        $mailinglist = 0;
    }
    
    
    if($password != ""){
        $password = md5($password);
        $query = $mysqli->prepare("UPDATE `cal_users` SET `role`= ?,`username`= ?,`password`= ?,`email`= ?,`mailinglist`= ? WHERE `id` =  ?");
        $query->bind_param(isssii, $role, $username, $password, $email, $mailinglist, $id);
    } else {
        $query = $mysqli->prepare("UPDATE `cal_users` SET `role`= ?,`username`= ?,`email`= ?,`mailinglist`= ? WHERE `id` = ?");
        $query->bind_param(issii, $role, $username, $email, $mailinglist, $id);
    }
    $query->execute();
    $query->store_result();
    
    //if query is done
    if($query){
        ?>
        <div class="alert alert-success">
            The data is saved successfully
        </div>
        <?php
    } else {
        ?>
        <div class="alert alert-error">
            There was an error during the saving, please try again
        </div>
        <?php
    }
}

//get current user data
$query = $mysqli->prepare("SELECT 
                            `cal_users`.`id`,
                            `cal_users`.`role`,
                            `cal_users`.`username`,
                            `cal_users`.`email`,
                            `cal_users`.`mailinglist`
                           FROM 
                            `cal_users` 
                           WHERE 
                            `id` = ?
                         ");
$query->bind_param(i, $id);
$query->bind_result($id, $role, $username, $email, $mailinglist);
$query->execute();

while($query->fetch()){
    $role = $role;
    $username = $username;
    $email = $email;
    $mailinglist = $mailinglist;
}

?>
<form class="form-horizontal" method="post" action="">
  <fieldset>
    <legend>Edit user</legend>
    <div class="control-group">
      <label class="control-label" for="role">Role</label>
      <div class="controls">
        <select class="span3" name="role">
            <option value="1" <?php if($role == '1'){ echo "selected"; } else { } ?> >User</option>
            <option value="2" <?php if($role == '2'){ echo "selected"; } else { } ?> >Administrator</option>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="username">Username</label>
      <div class="controls">
        <input type="text" class="input-xlarge" id="username" name="username" value="<?php echo $username; ?>">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="password">Password</label>
      <div class="controls">
        <input type="text" class="input-xlarge" id="password" name="password" placeholder="empty for no changes">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="email">E-Mail</label>
      <div class="controls">
        <input type="text" class="input-xlarge" id="email" name="email"  value="<?php echo $email; ?>">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="mailinglist">Mailinglist</label>
      <div class="controls">
        <input type="checkbox" name="mailinglist" <?php if($mailinglist == '1'){ echo "checked=checked"; } ?> />
      </div>
    </div>
    <div class="control-group">
      <div class="controls">
            <button type="submit" class="btn btn-primary" name="submit">Save changes</button>
            <a href="<?php echo $url; ?>user_delete.php?id=<?php echo $id; ?>" class="btn btn-danger">Delete</a>
      </div>
    </div>
  </fieldset>
</form>
<?php

include_once 'includes/footer.inc.php'; ?>
Return current item: PHP Address book