回答です

This commit is contained in:
a-kizaki
2026-10-28 16:53:31 +09:00
parent 06c1988850
commit fcaf9d4b5f
28 changed files with 10572 additions and 0 deletions

34
answer/Ex03.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
// Ex03_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);
$sql = "INSERT INTO employees (name, dep_id, project_id, salary)
VALUES (:name, :dep_id, :project_id, :salary)";
$stmt = $pdo->prepare($sql);
// サンプルデータの設定
$name = "山田一郎";
$dept_id = 1;
$project_id = 2;
$salary = 250000;
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':dep_id', $dept_id, PDO::PARAM_INT);
$stmt->bindParam(':project_id', $project_id, PDO::PARAM_INT);
$stmt->bindParam(':salary', $salary, PDO::PARAM_INT);
$stmt->execute();
echo "レコードを挿入しました";
} catch (PDOException $e) {
echo "エラー: " . $e->getMessage();
} finally {
$pdo = null;
}