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


accessing the user profile by clicking a link

美好的一天!我正在建立一个在线学习网站,每个用户都有自己的帐户,每当他们点击它时,他们都可以通过"我的个人资料"的链接查看他们的个人资料。我尝试连接它以登录,但它直接进入个人资料页面。这是我的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!";
}
}
?>

我会告诉你我会怎么做:首先,我将创建一个配置.php和函数.php,我将包含在index.php中,这是我的配置.php:

<?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();
     }
?>

这是我的函数.php:

<?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;
     }
?>

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

<?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";
     }
?>

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

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