回答です
This commit is contained in:
25
answer/Ex04.php
Normal file
25
answer/Ex04.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
// Ex04_answer.php
|
||||
|
||||
$dsn = "mysql:host=localhost;dbname=employee_db;charset=utf8";
|
||||
$username = "rezouser";
|
||||
$password = "Rezo_0000";
|
||||
|
||||
try {
|
||||
$pdo = new PDO($dsn, $username, $password);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
$salary = isset($_POST['salary']) ? (float)$_POST['salary'] : 1000;
|
||||
$id = isset($_POST['id']) ? (int)$_POST['id'] : 1;
|
||||
|
||||
$stmt = $pdo->prepare("UPDATE employees SET salary = :salary WHERE id = :id");
|
||||
$stmt->bindParam(':salary', $salary, PDO::PARAM_INT);
|
||||
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
|
||||
echo "更新しました";
|
||||
} catch (PDOException $e) {
|
||||
echo "エラー: " . $e->getMessage();
|
||||
} finally {
|
||||
$pdo = null;
|
||||
}
|
||||
Reference in New Issue
Block a user