通过单击链接访问用户配置文件

时间:2015-02-01 14:26:16

标签: php html phpmyadmin

美好的一天!我正在建立一个在线学习网站,其中每个用户都有自己的帐户,只要他们点击它,他们就可以通过“我的个人资料”链接查看他们的个人资料。我已尝试将其连接到登录,但它直接进入配置文件页面。这是我的instructor_profile.php代码,如何将教师菜单中的链接(我的个人资料)连接到个人资料页面?谢谢。

<?php  

 mysql_connect("localhost", "root", "root");    
 mysql_select_db("db_elearning");

 $idNumber = $_REQUEST['idNumber'];  

 $get = mysql_query("SELECT * FROM tbl_instructor WHERE idNumber = '$idNumber'");      
 $get2 = mysql_fetch_assoc($get);  
 $username = $get2['username'];   
 $password = $get2['password'];   
 $lastName = $get2['lastName'];   
 $firstName = $get2['firstName'];    
 $middleName = $get2['middleName'];  
 $location = $get2['location'];    


?>  
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body,td,th {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 18px;
}
</style>
</head>

<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><br />
</p>
<form id="form1" name="form1" method="post" action="">
  <p align="center"><strong>Instructor's profile</strong> </p>
  <table border="0" width="30%" align="center">
  <tr><td width="40%">
    <label for="username">Username</label> </td> <td>
    <input type="text" name="username" id="username" value="<?php echo $username;?>" />
  </td> </tr>
  <tr><td width="40%">
    <label for="password">Password</label> </td> <td>
    <input type="text" name="password" id="password" value="<?php echo $password;?>" />
  </td> </tr>
    <tr><td width="40%">
    <label for="lastName">Last name</label> </td> <td>
    <input type="text" name="lastName" id="lastName" value="<?php echo $lastName;?>" />
  </td> </tr>
  <tr><td width="40%">
    <label for="firstName">First name</label> </td> <td>
    <input type="text" name="firstName" id="firstName" value="<?php echo $firstName;?>" />
  </td> </tr>
  <tr><td width="40%">
    <label for="middleName">Middle name</label> </td> <td>
    <input type="text" name="middleName" id="middleName" value="<?php echo $middleName;?>" />
  </td> </tr>
  </td> </tr>
  <tr><td width="40%">
    <label for="location">Location</label> </td> <td>
    <input type="text" name="location" id="location" value="<?php echo $location;?>" />
  </td> </tr>
  </table>
</form>
</body>
</html>

这是教师的登录,它直接转到用户的个人资料

<?php

$username  = $_POST['username'];
$password = $_POST['password'];

if($username && $password){

    mysql_connect("localhost", "root", "root") or die("Connection to server failed!");
    mysql_select_db("db_elearning");

    $query = mysql_query("SELECT * FROM tbl_instructor WHERE username ='$username'");
    $numrows = mysql_num_rows($query);

    if($numrows != 0){

        while($row = mysql_fetch_assoc($query)){

            $idNumber = $row['idNumber'];
            $dbname = $row['username'];
            $dbpassword = $row['password'];

        }
        if($username==$dbname){
            if($password==$dbpassword){

                header("location:instructor_frame.html");

            if ($numrows ==1){
                header("location:instructor_profile.php?idNumber=$idNumber");
            }else{
                echo "Your password is incorrect!";
            }

        }else{
            echo "Your name is incorrect!";
        }

    }else{
        echo "This name is not registered!";    
    }

}else{
    echo "You have to type a name and password!";
}
}
?>

1 个答案:

答案 0 :(得分:0)

我会告诉你我将如何做到这一点: 首先,我将创建一个config.php和functions.php,我将包含在index.php中, 这是我的config.php:

&#13;
&#13;
<?php
     $link = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
     // Check connection
     if (!$link){
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
?>
&#13;
&#13;
&#13;

这是我的functions.php:

&#13;
&#13;
<?php
     function getInstructorsProfileById($id){
          global $link;
          $sql = "SELECT * FROM 'tbl_instructor' WHERE 'idNumber' = '$idNumber'";
          $res = mysqli_query($link,$sql);
          $row=mysqli_fetch_assoc($res);
          return $row;
     }
?>
&#13;
&#13;
&#13;

(警告在代码区域我不能使用我需要的引号类型,所以请注意,在表/列名称,$ sql内部,你必须使用那种类型的引号,在我的键盘上是键1的左侧,如果按下shift键,它也有〜) 我的index.php页面是:

&#13;
&#13;
<?php
    include 'config.php';//necesarily the first
    include 'function.php'; // so it would have the global $link variable to call
    if(isset($_GET['idNumber'])){ 
        $someVar = getInstructorsProfileById($_GET['idNumber']);
        extract($someVar);
?>
<form id="form1" name="form1" method="post" action="">
  <p align="center"><strong>Instructor's profile</strong> </p>
  <table border="0" width="30%" align="center">
    <tr><td width="40%">
      <label for="username">Username</label> </td> <td>
      <input type="text" name="username" id="username" value="<?php echo $username;?>" />
    </td> </tr>
    <tr><td width="40%">
      <label for="password">Password</label> </td> <td>
      <input type="text" name="password" id="password" value="<?php echo $password;?>" />
    </td> </tr>
    <tr><td width="40%">
      <label for="lastName">Last name</label> </td> <td>
      <input type="text" name="lastName" id="lastName" value="<?php echo $lastName;?>" />
  </td> </tr>
  <tr><td width="40%">
    <label for="firstName">First name</label> </td> <td>
    <input type="text" name="firstName" id="firstName" value="<?php echo $firstName;?>" />
  </td> </tr>
  <tr><td width="40%">
    <label for="middleName">Middle name</label> </td> <td>
    <input type="text" name="middleName" id="middleName" value="<?php echo $middleName;?>" />
  </td> </tr>
  </td> </tr>
  <tr><td width="40%">
    <label for="location">Location</label> </td> <td>
    <input type="text" name="location" id="location" value="<?php echo $location;?>" />
  </td> </tr>
  </table>
</form>
<?php }else{
          echo "some error if there is no number assigned";
     }
?>
&#13;
&#13;
&#13;

,此页面的链接将是: <?php $idNumber =7; // the number you know you need, example 7 ?> <a href="localhost/index.php?idNumber=<?php echo $idNumber; ?>">My Profile</a>

但这不是最好的方法,有更好的方法尝试在网页上搜索单词session_start(),php语法中的$ _SESSION 希望这有帮助