moviejunkie
IS-IT--Management
I need to be able to update a user from a form on a page, but I need a couple of variables for the form.
I need to be able to change the first name and last name of the user (this is no problem) the problem is when I want to change the password, If the user leaves the password text box empty it will not change the current password. Also if the user wants to change the password he/she must enter there current password and also type the new password twice to verify.
Here is the code I have so far
Thanks for your help.
I need to be able to change the first name and last name of the user (this is no problem) the problem is when I want to change the password, If the user leaves the password text box empty it will not change the current password. Also if the user wants to change the password he/she must enter there current password and also type the new password twice to verify.
Here is the code I have so far
Code:
<?php
require_once ('db.php');
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$password = clean($_POST['newpass']);
$passwordchk = clean($_POST['retypepass']);
$first_name = clean($_POST['first_name']);
$last_name = clean($_POST['last_name']);
$id = clean($_POST['id']);
$cpass = md5(clean($_POST['currpass']));
$sql ="";
$res="";
$newpass="";
$sql = "SELECT password FROM mis_users WHERE id = '$id'";
$res = mysql_query($sql) or die(mysql_error());
if ($res)
{
$currentPass = mysql_fetch_assoc($res);
$res="";
$sql="";
if ($currentPass["password"] != $cpass) {
header("location: ../setup.php?code=5");
exit();
}
if($password != $passwordchk){
header("location: ../setup.php?code=6");
exit();
}
else
{
$newpass = md5($password);
$sql="UPDATE mis_users SET first_name='$first_name', last_name='$last_name', password = '$newpass' WHERE id='id'";
$res = mysql_query($sql) or die(mysql_error());
$sql="";
if ($res)
{
$res="";
header("location: ../setup.php?code=3");
exit();
}
else {
$res="";
die("Query failed");
header("location: ../setup.php?code=4");
exit();
}
}
if($password == ""){
$sql="UPDATE mis_users SET first_name='$first_name', last_name='$last_name' WHERE id='$id'";
$res = mysql_query($sql) or die(mysql_error());
$sql="";
if ($res)
{
$res="";
header("location: ../setup.php?code=3");
exit();
}
else {
$res="";
die("Query failed");
header("location: ../setup.php?code=4");
exit();
}
}
}
else {
die("Query failed");
header("location: ../setup.php?code=2");
}
Thanks for your help.