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

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

$id = $database->escape($_GET['id']);

//when posted
if(isset($_POST['submit'])){
    $role = $_POST['role'];
    $username = $database->escape($_POST['username']);
    $email = $database->escape($_POST['email']);
    $password = $database->escape($_POST['password']);
        
    if($_POST['mailinglist'] == 'on'){
        $mailinglist = 1;
    } else {
        $mailinglist = 0;
    }
    
    
    if($password != ""){
        $password = md5($password);
        $q = $database->query("UPDATE `cal_users` SET `role`='$role',`username`='$username',`password`='$password',`email`='$email',`mailinglist`='$mailinglist' WHERE `id` = '$id'");
    } else {
        $q = $database->query("UPDATE `cal_users` SET `role`='$role',`username`='$username',`email`='$email',`mailinglist`='$mailinglist' WHERE `id` = '$id'");
    }
    
    //if query is done
    if($q){
        ?>
        <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 = $database->query("SELECT * FROM `cal_users` WHERE `id` = '$id'");
while($row = mysql_fetch_assoc($query)){
    $role = $row['role'];
    $username = $row['username'];
    $email = $row['email'];
    $mailinglist = $row['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