Skip to content

Instantly share code, notes, and snippets.

@sakibrahmanchy
Created November 15, 2020 08:17
Show Gist options
  • Select an option

  • Save sakibrahmanchy/859fd7dbcc1dfe9880cd91875ec76e2b to your computer and use it in GitHub Desktop.

Select an option

Save sakibrahmanchy/859fd7dbcc1dfe9880cd91875ec76e2b to your computer and use it in GitHub Desktop.
class UserController {
private $input = array(
2 => [
'user_id' => 2,
'user_name' => 'Mr. Abdul Kalam',
'age' => 53,
]
);
private $user_id = 2;
function add() {
if (isset($this->$input[$this->user_id])) {
$user_name = $this->input[$this->user_id]['user_name'];
$age = $this->input[$this->user_id]['age'];
$connection = new PDO("mysql:host=localhost;dbname=userdatabase", 'root', '1234');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $connection->prepare("INSERT INTO users(id, username, age)
VALUES (:id, :username , :age)");
$stmt->bindParam(':id', $this->user_id);
$stmt->bindParam(':username', $user_name);
$stmt->bindParam(':age', $age);
$stmt->execute();
}
}
function update() {
if (isset($this->$input[$this->user_id])) {
$user_name = $this->input[$this->user_id]['user_name'];
$age = $this->input[$this->user_id]['age'];
$connection = new PDO("mysql:host=localhost;dbname=userdatabase", 'root', '1234');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $connection->prepare("UPDATE users SET username=:username, age=:age where id=:id ");
$stmt->bindParam(':id', $this->user_id);
$stmt->bindParam(':username', $user_name);
$stmt->bindParam(':age', $age);
$stmt->execute();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment