每当我更改用户名时,我的系统就会出现问题。在我的数据库中,用户名是主键。请参见下面的屏幕截图。这就是它所做的。
如果我退出登录,一切都会显示出来。似乎每次我更改用户名时都无法连接到数据库。
这是我更新配置文件的代码:
<?php
session_start();
require 'config.php';
$user=$_SESSION['lec'];
$update = $conn->real_escape_string($_POST['update']);
$phone = $conn->real_escape_string($_POST['phone']);
$email = $conn->real_escape_string($_POST['email']);
$pswd = $conn->real_escape_string($_POST['pswd']);
$pswd2 = $conn->real_escape_string($_POST['pswd2']);
$name = $conn->real_escape_string($_POST['name']);
$username = $conn->real_escape_string($_POST['username']);
$dept = $conn->real_escape_string($_POST['dept']);
if ($pswd!=$pswd2)
{
$_SESSION["message"] = "Passwords do not match. Please try again.";
header("location:lecprofile?error");
}
else
{
$pswd = password_hash($pswd, PASSWORD_BCRYPT);
$sql1= "UPDATE lecturer SET email='$email' WHERE username='$user'";
$sql2 = "UPDATE lecturer SET phonenumb='$phone' WHERE username='$user'";
$sql4 = "UPDATE lecturer SET username='$username' WHERE username='$user'";
$sql5 = "UPDATE lecturer SET name='$name' WHERE username='$user'";
$sql6 = "UPDATE lecturer SET dept='$dept' WHERE username='$user'";
$result = $conn->query($sql1) && $conn->query($sql2) && $conn->query($sql4) && $conn->query($sql5) && $conn->query($sql6);
if (empty($pswd) || empty($pswd2))
{
header("location:lecprofile");
}
else
{
$sql3= "UPDATE lecturer SET pswd='$pswd' WHERE username='$user'";
}
$passresult=$conn->query($sql3);
if($result==TRUE || $passresult== TRUE)
{
$_SESSION['message'] = "Profile Updated!";
header("location:lecprofile?done");
}
}
if(isset($_POST['submit'])){
//Targeting Folder
$target="propics/";
$target=$target.basename($_FILES['propic']['name']);
//Getting Selected image Type
$type=pathinfo($target,PATHINFO_EXTENSION);
//Allow Certain File Format To Upload
if($type!='jpg' && $type!='jpeg' && $type!='JPG' && $type!='PNG' && $type!='GIF' && $type!='png' && $type!='gif'){
echo "Only JPG,JPEG,PNG and GIF file format are allowed to Upload";
}
else{
//lmit file size
if ($_FILES['propic']['size'] > 5000000) {
echo "Sorry, your image is too large.";
}
else{
//checking for Exsisting image Files
if(file_exists($target)){
echo "File Already Exist";
}else{
//Moving The image file to Desired Directory
$upload_success=move_uploaded_file($_FILES['propic']['tmp_name'],$target);
if($upload_success==TRUE){
//Getting Selected image Information
//renaming the file to the username to avoid conflict when uploading
$name=$user.".".$type;
rename("propics/".$_FILES['propic']['name'],"propics/".$name);
//
$sql= "UPDATE lecturer SET propic='$name' WHERE username='$user'";
$result = $conn->query($sql);
if($result==TRUE){
clearstatcache();
$_SESSION['message']= "Profile picture updated successfully!";
header("location:lecprofile.php?done");
}
}
}
}
}
}
?>
用于在数据库中显示数据的代码。
<?php
require 'config.php';
if ($_SESSION['logged_in'] != 1 ){
$_SESSION['message'] = "Please login!";
header("location: /prac?error");
}
else
{
$user=$_SESSION['lec'];
}
if (isset($_GET['done']))
{
include 'done.php';
}
if (isset($_GET['error']))
{
include 'error.php';
}
?>
<table >
<tr>
<td width="20%" align="center">
<?php $sql = "SELECT propic FROM lecturer WHERE username='$user'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
?>
<img id="mainpropic" src="propics/<?php echo $row['propic'];?>" width="200px" height="200px" >
</td>
<td >  </td>
<td>
<form action="" method="post">
<table cellpadding="2" >
<tr>
<td>
User Name:
</td>
<td>
<?php echo $_SESSION['lec'];?>
</td>
</tr>
<tr>
<td>
EC Number:
</td>
<td>
<?php
$sql = "SELECT ecnumber FROM lecturer WHERE username='$user'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["ecnumber"];?>
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<?php
$sql = "SELECT name FROM lecturer WHERE username='$user'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["name"];
?>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<?php
$sql = "SELECT email FROM lecturer WHERE username='$user'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["email"];?>
</td>
</tr>
<tr>
<td>
Phone Number:
</td>
<td>
<?php
$sql = "SELECT phonenumb FROM lecturer WHERE username='$user'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["phonenumb"];?>
</td>
</tr>
<tr>
<td>
Department:
</td>
<td>
<?php
$sql = "SELECT dept FROM lecturer WHERE username='$user'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["dept"];?>
</td>
</tr>
<tr>
</tr>
<tr>
<td>
<input type="button" value="Edit Profile" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#Modal">
</td>
<td>
</td>
</tr>
</table>
2条答案
按热度按时间k4emjkb11#
我在代码中修复了这个错误。我将新用户名分配给该帐户的全局$\u会话变量,如下所示:
kknvjkwl2#
进行更新时,您使用的是会话中存储的当前用户名。更新完成后,用户名会发生更改,因此用于读取数据的旧用户名不再存在于数据库中。当您注销并重新登录时,会话变量lec将更新为新用户名,使您能够读取数据。
尝试使用在更新时不会更改的主键。