My error when I try to change the name:
Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE userId = 2' at line 1 in C:\xampp\htdocs\login2\includes\functions.inc.php:148 Stack trace: #0 C:\xampp\htdocs\login2\includes\functions.inc.php(148): mysqli->query('UPDATE users2 S...') #1 C:\xampp\htdocs\login2\includes\editfac.inc.php(23): updateFacName(Object(mysqli), '2', 'Min, Shan') #2 {main} thrown in C:\xampp\htdocs\login2\includes\functions.inc.php on line 148
My error when I try to change the username:
Fatal error: Uncaught mysqli_sql_exception: Unknown column 'shan' in 'field list' in C:\xampp\htdocs\login2\includes\functions.inc.php:167 Stack trace: #0 C:\xampp\htdocs\login2\includes\functions.inc.php(167): mysqli->query('UPDATE users2 S...') #1 C:\xampp\htdocs\login2\includes\editfac.inc.php(26): updateUsername(Object(mysqli), '1', 'shan') #2 {main} thrown in C:\xampp\htdocs\login2\includes\functions.inc.php on line 167
My Code in includes/function.inc.php:
<?php
function emptyInputEditFac($facid, $name, $username) {
$result;
if (empty($facid) && empty($name) && empty($username)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function updateFacId($conn, $userid, $facid) {
$sql = "UPDATE users2 SET facId = $facid WHERE userId = $userid;";
if ($conn -> query($sql) === TRUE) {
echo "Record updated successfully";
header("location: ../faculty.php");
exit();
}
else {
phpalert("Error updating record: " . $conn->error);
header("location: ../faculty.php");
exit();
}
}
function updateFacName($conn, $userid, $name) {
$sql = "UPDATE users2 SET name = $name WHERE userId = $userid;";
if ($conn -> query($sql) === TRUE) {
echo "Record updated successfully";
header("location: ../faculty.php");
exit();
}
else {
echo("Error updating record: " . $conn->error);
header("location: ../faculty.php");
exit();
}
}
function updateUsername($conn, $userid, $username) {
$sql = "UPDATE users2 SET username = $username WHERE userId = $userid;";
if ($conn -> query($sql) === TRUE) {
echo "Record updated successfully";
header("location: ../faculty.php");
exit();
}
else {
phpalert("Error updating record: " . $conn->error);
header("location: ../faculty.php");
exit();
}
}
My Code in includes/editfac.inc.php:
<?php
if (isset($_POST['submit'])) {
$userid = $_POST["userid"];
$facid = $_POST["facid"];
$name = $_POST["name"];
$username = $_POST["username"];
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if (emptyInputEditFac($facid, $name, $username) !== false) {
header("location: ../faculty.php?change=nochange");
exit();
}
else {
if (!empty($facid)) {
updateFacId($conn, $userid, $facid);
}
if (!empty($name)) {
updateFacName($conn, $userid, $name);
}
if (!empty($username)) {
updateUsername($conn, $userid, $username);
}
exit();
}
}
else {
header("location: ../faculty.php");
exit();
}
My Code in editfac.php:
<section class = "form-sec">
<div class = "form-div">
<form id="editfac-form" action="includes/editfac.inc.php" method="post">
<?php
if (isset($_POST['user-id'])) {
$userId = $_POST['user-id'];
echo "<input type='hidden' name='userid' value='".$userId."'/>";
}
?>
<input type="number" name="facid" placeholder="Faculty ID Number">
<input type="text" name="name" placeholder="Lastname, Firstname MI.">
<input type="text" name="username" placeholder="Username">
<button type="submit" name="submit">Update</button>
</form>
</div>
</section>
The updateFacId() function works fine for some reason.
What I have tried:
Searched about the error in Google but I couldn't understand much.