I wanted to ask your opinion about my code created for acccount.php page for editing the data of each logged-in user.
I wanted to have more of an opinion on your right syntax, though maybe it could be improved.
require('includes/config.php');
//if not logged in redirect to login page
if(!$user->is_logged_in()) {
redirect('login.php');
}
//define page title
$title = 'Account';
//include header template
require('layout/header.php');
//submit post
if (isset($_POST['location'], $_POST['bio'], $_POST['relationship'], $_POST['interested'], $_POST['work'], $_SESSION['username'])) {
try {
//query update
$stmt = $db->prepare("UPDATE members SET location = ?, bio = ?, relationship = ?, interested = ?, work = ? WHERE username = ?");
$stmt->execute([$_POST['location'], $_POST['bio'], $_POST['relationship'], $_POST['interested'], $_POST['work'], $_SESSION['username']]);
$message = $lang['update-profile-success']; //success message
} catch(PDOException $e) {
$message = $lang['update-profile-error']; //error message
}
}
// ****************** AVATAR *************************
//choosing the avatar:
if(isset($_POST['upload']))
{
$imgFile = $_FILES['avatar']['name'];
$tmp_dir = $_FILES['avatar']['tmp_name'];
$imgSize = $_FILES['avatar']['size'];
if(empty($imgFile)){
$message = "Please Select Image File.";
}
else
{
$upload_dir = 'avatar/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
// rename uploading image
$userpic = rand(1000,1000000).".".$imgExt;
// allow valid image file formats
if(in_array($imgExt, $valid_extensions)){
// Check file size '5MB'
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$message = "Sorry, your file is too large.";
}
}
else{
$message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
// if no error occured, continue ....
if(!isset($message))
{
$edit = $db->prepare("UPDATE members SET user_avatar = :avatar WHERE username ='" . $_SESSION['username'] . "'");
$edit->bindParam(':avatar', $userpic);
$row['user_avatar'] = $userpic;
if($edit->execute())
{
$message = "Avatar updated...";
header("refresh:5;account.php"); // redirects image view page after 5 seconds.
}
else
{
$message = "Error...";
}
}
}
//run data form
$sql = $db->prepare("SELECT * FROM members WHERE username = ?");
$sql->execute([$_SESSION['username']]);
$row = $sql->fetch();