I've tried everything and just cant seem to get this working.. it's probably a silly mistake I can't see but any help is appreciated.
As stated in the question I'm trying to insert records into a table via a form. I have a functions.php which includes my database.php with the pdo connection (all working fine) class with the following function in it:
function insertStaffUser($username, $password, $role) {
include('database.php');
try {
$query = "INSERT INTO users (userid, username, password, role) VALUES (default, :username, :password, :role)";
$stmt->$db->prepare($query);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':role', $role);
$result = $stmt->execute();
if($result) {
echo "INSERTED SUCCESSFULLY";
} else {
echo "error inserting";
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
And the following code is the one in my html class which is addUser.php
with 3 text fields (new username, password and role).
<?php
if(isset($_POST['submit'])) {
$new_username = $_POST['username'];
$new_pass = $_POST['password'];
$new_role = $_POST['role'];
insertStaffUser($new_username, $new_pass, $new_role);
}
?>
Can anyone see what's wrong with this or what I'm doing wrong, thanks for the help!