hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
hi
Hello! How can I assist you today?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
i want to save the profile once a user has saved their information while retaining the current values of all the fields that the user is not allowed to edit themselves: FORM:
<div class="form-group">
<legend>Password</legend>
<input type="" name="exPass" class="form-control" required="" value="<?php echo $selAccRow['exmne_password']; ?>">
</div>
<div class="form-group">
<legend>Confirm Password</legend>
<input type="" name="exPass2" class="form-control" required="" value="<?php echo $selAccRow['exmne_password']; ?>">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" onclick="validateForm()">
<i class="fas fa-paper-plane"></i> Update Profile
</button>
</div>
</div>
</form>
$.ajax({
url: "query/update-profile.php",
type: "POST",
data: $(this).serialize(),
dataType: "json",
success: function(data) {
if (data.res == "success") {
Swal.fire({
title: "Success",
html: data.exFullname + " <br>has been successfully updated!",
icon: "success"
}).then(function() {
location.reload(); // Reload the page to show updated information
});
}
}
});
});
}); Here is an example of how i am using PHP on the same app to allow the admin to save the edited profile on the admin section, i want to refactor the code to work as stated earlier. <?php include("../../../conn.php");
_POST['exmne_id']; _POST['exFullname']; _POST['exEmail']; _POST['exPhone']; _POST['exPass']; _POST['exPass2']; _POST['exmne_package']; _POST['exmne_both']; _POST['exmne_status'];
if (exPass2) { exPass; } else { res); exit; }
if (exmne_status == 'PAID') { exmne_subscription = date('Y-m-d');
switch ($exmne_package) {
case "PREP COURSES MONTHLY":
case "NURSING SCHOOL MONTHLY":
$exmne_expiry = date('Y-m-d', strtotime($exmne_subscription . "+30 days"));
break;
case "PREP COURSES 6 MONTHS":
case "NURSING SCHOOL 6 MONTHS":
$exmne_expiry = date('Y-m-d', strtotime($exmne_subscription . "+180 days"));
break;
case "PREP COURSES 1 YEAR":
case "NURSING SCHOOL 1 YEAR":
$exmne_expiry = date('Y-m-d', strtotime($exmne_subscription . "+365 days"));
break;
case "NURSING SCHOOL 2 YEARS":
$exmne_expiry = date('Y-m-d', strtotime($exmne_subscription . "+730 days"));
break;
default:
$exmne_expiry = null;
break;
}
} else { exmne_expiry = null; }
conn->prepare("UPDATE examinee_tbl SET exmne_fullname=:exFullname, exmne_email=:exEmail, exmne_phone=:exPhone, exmne_password=:hashedPassword, exmne_package=:exmne_package, exmne_both=:exmne_both, exmne_subscription=:exmne_subscription, exmne_expiry=:exmne_expiry, exmne_status=:exmne_status WHERE exmne_id=:exmne_id"); exFullname); exEmail); exPhone); hashedPassword); exmne_package); exmne_both); exmne_subscription); exmne_expiry); exmne_status); exmne_id);
if (updCourse->execute()) { res = array("res" => "success", "exFullname" => exFullname); } else { res = array("res" => "failed"); }
header("Content-Type: application/json"); echo json_encode($res); ?>
To maintain the current values of fields that a user is not allowed to edit themselves while still updating their profile based on the fields they are allowed to modify, you'll need to make a few adjustments to your PHP script. The main idea here is to fetch the current user data and only update the fields the user can edit. If any non-editable fields are part of the form submission, you should ignore these and use the current values from the database instead.
Let's refactor your update logic considering that the user is not allowed to edit exmne_package, exmne_both, exmne_subscription, exmne_expiry, and exmne_status directly, and we'll assume these should be maintained as is in the database without being affected by the form submission:
include("../../../conn.php");
// Ensure the user is identified.
$exmne_id = $_POST['exmne_id'];
if (empty($exmne_id)) {
echo json_encode(["res" => "noID"]);
exit;
}
// Fetch the current data for the user.
$stmt = $conn->prepare("SELECT * FROM examinee_tbl WHERE exmne_id=:exmne_id");
$stmt->bindParam(':exmne_id', $exmne_id, PDO::PARAM_INT);
$stmt->execute();
$currentUser = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$currentUser) {
echo json_encode(["res" => "userNotFound"]);
exit;
}
// Data from the form.
$exFullname = $_POST['exFullname'];
$exEmail = $_POST['exEmail'];
$exPhone = $_POST['exPhone'];
$exPass = $_POST['exPass'];
$exPass2 = $_POST['exPass2'];
// Password matching and hashing.
if ($exPass !== $exPass2) {
echo json_encode(["res" => "passwordMismatch"]);
exit;
}
$hashedPassword = password_hash($exPass, PASSWORD_DEFAULT);
// Update query maintains the values not editable by the user.
$updCourse = $conn->prepare("UPDATE examinee_tbl SET exmne_fullname=:exFullname, exmne_email=:exEmail, exmne_phone=:exPhone, exmne_password=:hashedPassword WHERE exmne_id=:exmne_id");
$updCourse->execute([
':exFullname' => $exFullname,
':exEmail' => $exEmail,
':exPhone' => $exPhone,
':hashedPassword' => $hashedPassword,
':exmne_id' => $exmne_id
]);
if ($updCourse->rowCount() > 0) {
echo json_encode(["res" => "success", "exFullname" => $exFullname]);
} else {
echo json_encode(["res" => "failed"]);
}
A few key points in this refactor:
exmne_id is provided. This is critical for updating the right user profile.password_hash for hashing the password, which is a best practice for security. Ensure the hash is compatible with your login mechanism.exmne_package, exmne_both, exmne_subscription, exmne_expiry, exmne_status) are not part of this update statement, assuming these are not supposed to be edited through this form.Before deploying these changes, ensure to test thoroughly, especially the password handling part to make sure it aligns with your login logic concerning password verification.