tags:

views:

17

answers:

1

situation: i have changepass page to change password. but the page cannot retrieve and display the value in the database. i want to display the ID, name and department that have been stored in the db. the newpasswword also cannot be update...plz..help me..

here's the code:

<?php 
    session_start();
    $username = $_SESSION["username"];

?>
<?php 
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("fyp", $con);

    $username=$_SESSION["username"];


    $query = "SELECT * from access WHERE username = '$username'";
    $result = @mysql_query($query);
    $row = mysql_fetch_array($result);
    $username           =   $row["username"];
    $name               =   $row["name"];
    $department         =   $row["department"];

    mysql_query($query) or die ("Query Failed".mysql_error());

    //mysql_close($link);


if(isset($_POST['submit']))
{
if (!$_POST['newpassword'])
        {
            echo "<script language='Javascript'>alert(' Please Enter The New Password');</script>";
        } 
    else
    {
    $newpassword=$_POST['newpassword'];
    if(!eregi("^[[:alnum:]]{6,12}$", $newpassword)) 
        { 
            echo "<script language='Javascript'>alert(' New Password must be 6-12 element');</script>";
        }
    else {
    $query1 = "UPDATE access SET password=$newpassword WHERE username = '$username'";
    mysql_query($query1) or die ("Query Failed".mysql_error());

  echo "<script> alert('Change Password Success. Please Login With The New Password.');
    document.location.href='login.php?mosmsg=Please enter the value'</script>\n";
        }
    }
}
?>  

<font face= "arial" size="2" font color="black">
<center>
<h3 align=center> Change Password </h3>
<table width="500" height="100" border="0" cellspacing="0" cellpadding="2">

<tr>
      <tr>
           <td align="left">User ID</td>
           <td>: <? {echo "$username"; } ?></td>
      </tr>  

      <tr>
        <td align="left">Name </td>
        <td>: <? {echo "$name"; } ?></td>
    </tr>       

         <tr>
           <td align="left">Department  </td>
           <td>: <?php echo $row['department']; ?> </td>      
         </tr>

         <tr>
           <td align="left">New Password </td>
           <td>: <input name="newpassword" type="password" id="newpassword" size="20" ></td>
      </tr>
+1  A: 

You are using mysql_query function two times which is wrong, remove this line:

mysql_query($query) or die ("Query Failed".mysql_error());

Your code should look like this:

$query = "SELECT * from access WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$username           =   $row["username"];
$name               =   $row["name"];
$department         =   $row["department"];

This way you will come to know if there is any erorr from mysql and proceed further depending on that error if one is there. Let's know if this works or there is a mysql error.

EDIT: Couple of things, first you are missing the form tag in your html, two put these lines at the start of the script to know what errors you receive.

ini_set('display_errors', true);
error_reporting(0);
Sarfraz
i've removed the statement and replace the code with yours..but it still on the same situation. the value not displayed and when i click on the submit button, it become blank..
@ejah85: i have updated my answer, check the Edit part. Thanks
Sarfraz
i've follow your suggestion...but it seem not work...it still on the same situation..i dont know what wrong with the code..
@ejah85: hmmm, then you will have to apply divide and rule methodology to debug your code, remove some code then see and so to see what is the faulty code.
Sarfraz
im php newbie..i don't know how to trace the error