Skip to content
Snippets Groups Projects
Commit c6d09e83 authored by Iqbal's avatar Iqbal
Browse files

Added GetUserBy and InsertUser method.

parent 21776382
Branches crud-methods
No related merge requests found
......@@ -55,9 +55,20 @@ class User {
$stmt = $conn->prepare("SELECT * FROM user WHERE id=?");
$stmt->execute([$id]);
$stmt->setFetchMode(PDO::FETCH_OBJ);
$user = $stmt->fetch();
$user = $stmt->fetchObject();
return $user;
} catch (PDOException $e) {
echo "Error: ".$e->getMessage();
return false;
}
}
public static function GetUserBy($attribute, $value, PDO $conn) {
try {
$stmt = $conn->prepare("SELECT * FROM user WHERE $attribute='$value'");
$stmt->execute();
$user = $stmt->fetchObject();
return $user;
} catch (PDOException $e) {
echo "Error: ".$e->getMessage();
......@@ -82,4 +93,32 @@ class User {
}
}
public static function InsertUser($user, PDO $conn) {
try {
$lastUser = $conn->query("SELECT * FROM user ORDER BY id DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC);
$newId = $lastUser['id'] + 1;
$values = "";
$columns = "";
foreach ($user as $key => $attr) {
if ($key !== "id" && $key !== "is_driver") {
$columns .= $key.",";
$values .= "'$attr'".",";
} else if ($key === "is_driver") {
$columns .= $key;
$values .= $attr;
}
}
$columns = "id,".$columns;
$values = $newId.",".$values;
$insertExpression = "INSERT INTO user ($columns) VALUES ($values)";
$conn->query($insertExpression);
} catch (PDOException $e) {
echo "Error: ".$e->getMessage();
return false;
}
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment