无法在数据库中检索值而无法更新表

时间:2010-02-28 07:51:16

标签: php mysql

情况: 我有changepass页面来更改密码。但页面无法检索并显示数据库中的值。我想显示已存储在数据库中的ID,名称和部门。 newpasswword也无法更新... plz..help me ..

这是代码:

<?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 个答案:

答案 0 :(得分:1)

您正在使用mysql_query两次错误的功能,请删除此行:

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

您的代码应如下所示:

$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"];

通过这种方式,您将了解是否存在来自mysql的任何eror并根据该错误继续进行,如果有的话。让我们知道这是否有效或是否存在mysql错误。

修改 有几件事情,首先你缺少html中的form标签,两个将这些行放在脚本的开头,以了解你收到的错误。

ini_set('display_errors', true);
error_reporting(0);