MySQL数据库行为不佳与PHP更新

时间:2018-10-02 18:55:55

标签: php mysql

每当更改用户名时,我的系统行为异常。在我的数据库中,用户名是主键。 请参见下面的屏幕截图。这是在做什么。 enter image description here

如果我注销登录名,则一切都会显示。每次更改用户名似乎都无法连接到数据库。

这是我用于更新个人资料的代码:

ByteArrayInputStream(tarInput.getCurrentEntry())

用于在数据库中显示数据的代码。

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


?>

1 个答案:

答案 0 :(得分:4)

进行更新时,您正在使用会话中存储的当前用户名。更新完成后,用户名将更改,因此用于读取数据的旧用户名不再存在于数据库中。当您注销并重新登录会话变量时,lec将更新为新的用户名,使您能够读取数据。

尝试使用在更新时不会更改的主键。