gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:15:31 25 4
gpt4 key购买 nike

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

最佳答案

我会告诉你我会怎么做:首先,我将创建一个 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";
}
?>

这个页面的链接是: <?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 等词希望这有帮助

关于php - 通过单击链接访问用户配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com