I have the following code that I am using to update two different tables in my database:
public function Set_add_user($first_name, $last_name, $email, $username, $password_check, $permissions) {
global $mysqli;
$user_id = '';
if ($stmt = $mysqli->prepare("INSERT INTO users (first_name, last_name, email, username, password) VALUES (?,?,?,?,?)")) {
$stmt->bind_param("sssss", $first_name, $last_name, $email, $username, $password_check);
if ($stmt->execute()) {
$this->users = 'Success';
} else {
$this->users = 'Failed at INSERT 1';
}
$stmt->close();
}
if ($stmt = $mysqli->prepare("SELECT user_id FROM users WHERE first_name = ? AND last_name = ? AND username = ?")) {
$stmt->bind_param("sss", $first_name, $last_name, $username);
$stmt->execute();
$stmt->bind_result($user_id);
$stmt->fetch();
$this->users = $user_id;
$stmt->close();
}
if ($stmt = $mysqli->prepare("UPDATE users SET permissions_id = ? WHERE user_id = ?")) {
$stmt->bind_param("ii", $user_id, $user_id);
if ($stmt->execute()) {
$this->users = 'Success';
} else {
$this->users = 'Failed at UPDATE 1';
}
$stmt->close();
}
$per_columns = $per_values = array();
foreach ($permissions as $key => $value) {
$per_columns[] = $key;
$per_values[] = 1;
}
$permissions_columns = implode(', ',$per_columns);
$permissions_values = implode(', ',$per_values);
if ($stmt = $mysqli->prepare("INSERT INTO permissions (user_id, ?) VALUES (?, ?)")) {
$stmt->bind_param("sis", $permissions_columns, $user_id, $permissions_values);
if ($stmt->execute()) {
$this->users = 'Success';
} else {
$this->users = 'Failed at INSERT 2';
}
$stmt->close();
}
return $this->users;
}
Everything works fine until I get down to the last prepare statement (“INSERT INTO permissions”…). I printed out the query by itself to make sure that it formats correctly, which gives me…
INSERT INTO permissions (user_id, per_users, per_organization) VALUES (7,1,1)
My database is set up so that user_id is an INT and per_users and per_organization are also INT’s.
Am I writing this out incorrectly? Any help would be much appreciated. It isn’t shooting me an error message, it is simply not adding the permissions into the database.