【问题标题】:accessing the user profile by clicking a link通过单击链接访问用户个人资料
【发布时间】:2015-03-31 13:45:18
【问题描述】:

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


【解决方案1】:

我会告诉你我会怎么做: 首先,我将创建一个 config.php 和 functions.php,我将它们包含在 index.php 中, 这是我的 config.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();
     }
?>

这是我的functions.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 ) 我的 index.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";
     }
?>

到这个页面的链接是: &lt;?php $idNumber =7; // the number you know you need, example 7 ?&gt; &lt;a href="localhost/index.php?idNumber=&lt;?php echo $idNumber; ?&gt;"&gt;My Profile&lt;/a&gt;

但这不是最好的方法,为了有更好的方法尝试在网络上搜索 php 语法中的 session_start()、$_SESSION 等词 希望这会有所帮助

【讨论】:

  • 谢谢,但我遇到了这个问题:找不到对象!在此服务器上找不到请求的 URL。引用页面上的链接似乎是错误的或过时的。请将该错误通知该页面的作者。如果您认为这是服务器错误,请联系网站管理员。错误 404 localhost Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3
猜你喜欢
  • 1970-01-01
  • 2015-05-03
  • 2012-08-23
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 2012-09-06
相关资源
最近更新 更多