<?php
include('../headertop.php');
//include('../datepicker.php');
include('../db.php');

$disp = '';
$pagetitle = 'Edit Profile';

if(!isset($_POST['editprofilesubmit']))
{
echo '<meta http-equiv="refresh" content="0;URL=editprofile.php">';
exit;
}

$userid = $_POST['userid'];
$username = $_POST['username'];
$password = $_POST['password'];
$old_password = $_POST['old_password'];
//$username = $_SESSION['user'];
$password2 = $_POST['password2'];

if(empty($username) or empty($userid) or empty($password) or empty($password2) or empty($old_password))
{
$disp .= '<span class="error">Please fill all required fields</span>';
include('../template.php');
exit;
}

if($password != $password2)
{
$disp .= '<span class="error">New passwords do not match</span>';
include('../template.php');
exit;
}

$q = "select * from user where id='$userid'";
$res = mysqli_query($dbcon,$q) or die(mysqli_error($dbcon));
$row = mysqli_fetch_array($res);
if($row['password']!= $old_password)
{
echo '<meta http-equiv="refresh" content="0;URL=editprofile.php?sm=Old password is incorrect">';
exit;
}

$q = "update user set password='$password', username='$username' where id='$userid'";
mysqli_query($dbcon,$q) or die(mysqli_error($dbcon));

echo '<meta http-equiv="refresh" content="0;URL=login.php?sm=User details updated">';