Skip to content

Commit 8bc01da

Browse files
committed
fix(model): ensure all methods return consistent values on failure
1 parent ccd0f51 commit 8bc01da

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

‎src/models/User.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function getAllUser() : ?array {
2626
return $stmt->fetchAll(PDO::FETCH_ASSOC);
2727
} catch (PDOException $e) {
2828
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
29+
return null;
2930
}
3031
}
3132

@@ -43,6 +44,7 @@ public function getUserById($id) : ?array {
4344
return $stmt->fetchAll(PDO::FETCH_ASSOC);
4445
} catch (PDOException $e) {
4546
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
47+
return null;
4648
}
4749
}
4850

@@ -60,21 +62,22 @@ public function getUserByName($name) : ?array {
6062
return $stmt->fetchAll(PDO::FETCH_ASSOC);
6163
} catch (PDOException $e) {
6264
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');
65+
return null;
6366
}
6467
}
6568

6669
public function addNewUser($name, $age, $job) : void {
6770
try {
68-
$query = "INSERT INTO user (name, age, job) VALUES (?, ?, ?)";
71+
$query = "INSERT INTO user (name, age, job) VALUES (:name, :age, :job)";
6972

7073
if ($this->conn === null) {
7174
throw new Exception("Database connection is null.");
7275
}
7376

7477
$stmt = $this->conn->prepare($query);
75-
$stmt->bindParam(1, $name, PDO::PARAM_STR);
76-
$stmt->bindParam(2, $age, PDO::PARAM_STR);
77-
$stmt->bindParam(3, $job, PDO::PARAM_STR);
78+
$stmt->bindParam(":name", $name, PDO::PARAM_STR);
79+
$stmt->bindParam(":age", $age, PDO::PARAM_STR);
80+
$stmt->bindParam(":job", $job, PDO::PARAM_STR);
7881
$stmt->execute();
7982
} catch (PDOException $e) {
8083
error_log("Query failed: " . $e->getMessage(), 3, '../../logs/error.log');

0 commit comments

Comments
 (0)