I have a form set up on a users profile, so if they want to update items such as their first name, last name, username, or email then they can.
When they hit the submit on the form, a PHP script runs which essentially checks if any fields are not empty, and if there are not empty, run an update query to the db on the student
table. However, nothing seems to be happening, wondering if someone could point out where I have gone wrong as the student
table does not update?
profile.php:
<form action="scripts/update-profile.php" method="post">
<h3 class="left-align fontAmaticH1">Student Details</h3>
<p class="left-align"><b>Username: </b><?php echo $row['username']; ?>
<div class="update-profile"><input type="text" name="username" placeholder="Update Username..."></div>
</p>
<p class="left-align"><b>Email Address: </b><?php echo $row['email']; ?>
<div class="update-profile"><input type="text" name="email" placeholder="Update Email..."></div>
</p>
<p class="left-align"><b>First Name: </b><?php echo $row['firstName']; ?>
<div class="update-profile"><input type="text" name="firstName" placeholder="Update First Name..."></div>
</p>
<p class="left-align"><b>Surname: </b><?php echo $row['lastName']; ?>
<button name="update-details" class="update-details" type="submit">Update Details</button>
</form>
Edit Details
PHP:
<?php
// Checking whether the user got to this page by clicking the proper button.
if (isset($_POST['update-details'])) {
require 'db.php';
// We grab all the data which we passed from the update form
$studentID = $_SESSION['studentID'];
$username = $_POST['username'];
$email = $_POST['email'];
$profileImage = $_POST['profileImage'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$update = [];
if (! empty($username)) {
$update['username'] = "username ='".$username ."'";
}
if (! empty($email)) {
$update['email'] = "email='".$email ."'";
}
if (! empty($firstName)) {
$update['firstName'] = "firstName='".$firstName ."'";
}
if (! empty($lastName)) {
$update['lastName'] = "lastName='".$lastName ."'";
}
if (! empty($update)) {
$query = "UPDATE `student` SET ";
$query .= implode(', ', $update);
$query .= " WHERE `student`.`studentID` = $studentID ";
$result = $conn->query($query) or die ("update SQL error");
}
header("Location: ../profile.php?update=success");
}
?>