メンバー、学生、インストラクタの名前(名字と姓の組み合わせ)を入力できるフォームがあります。また、この質問に関係のない他のデータも入力します。これはすべてgames
という表に挿入されます。 members
、students
、およびinstructors
というテーブルがすでに存在し、それぞれにid
,first_name
およびlast_name
を含むフィールドがあります。必要なすべての外部キー制約が順番に行われます。だから、ユーザーがフォームを提出するとき、私はメンバー、学生、および/またはインストラクターの対応するIDを、名前ではなく、挿入するだけです。PHP MySQLの名前をIDに変換して挿入するとNULL値が返される
私は以下のコードを持っています。メンバー、学生、インストラクターのIDの他に、レコードにNULLとして表示されるすべての情報を正常に提出します。
誰かがこの問題をここで強調することができたら、私は大いに感謝します。
フォーム:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form</title>
</head>
<body>
<form action="action.php" method="post">
<p>
<p>
<label for="instructor_name">Instructor:</label>
<input type="text" name="instructor_name" id="instructor_name">
</p>
<p>
<label for="student_name">Student:</label>
<input type="text" name="student_name" id="student_name">
</p>
<p>
<label for="member_name">Member:</label>
<input type="text" name="member_name" id="member_name">
</p>
<p>
<label for="exercises">Exercises:</label>
<input type="text" name="exercises" id="exercises">
</p>
<p>
<label for="auth_by">Authorised By:</label>
<input type="text" name="auth_by" id="auth_by">
</p>
<p>
<label for="auth_duration">Authorisation Duration:</label>
<input type="text" name="auth_duration" id="auth_duration">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>
アクション:
<?php
// connection information
include 'pdo_config.php';
try {
// new pdo connection
$conn = new PDO($dsn, $user, $pass, $opt);
// post data
$member_name = $_POST['member_name'];
$exercises = $_POST['exercises'];
$auth_by = $_POST['auth_by'];
$auth_duration = $_POST['auth_duration'];
$student_name = $_POST['student_name'];
$instructor_name = $_POST['instructor_name'];
// get corresponding id's of names
$name_m = explode(" ", $member_name);
$first_name_m = $name_m[0];
$last_name_m = $name_m[1];
$statement1 = $conn->prepare("SELECT member_id FROM tbl_members WHERE first_name = :first_name AND last_name = :last_name");
$statement1->execute(array(':first_name' => $first_name_m, ':last_name' => $last_name_m));
$row1 = $statement1->fetch();
$member_id = $row1['member_id'];
$name_i = explode(" ", $instructor_name);
$first_name_i = $name_i[0];
$last_name_i = $name_i[1];
$statement2 = $conn->prepare("SELECT instructor_id FROM tbl_instructors WHERE first_name = :first_name AND last_name = :last_name");
$statement2->execute(array(':first_name' => $first_name_i, ':last_name' => $last_name_i));
$row2 = $statement2->fetch();
$instructor_id = $row2['instructor_id'];
$name_s = explode(" ", $student_name);
$first_name_s = $name_s[0];
$last_name_s = $name_s[1];
$statement3 = $conn->prepare("SELECT student_id FROM tbl_students WHERE first_name = :first_name AND last_name = :last_name");
$statement3->execute(array(':first_name' => $first_name_s, ':last_name' => $last_name_s));
$row3 = $statement3->fetch();
$student_id = $row3['student_id'];
// prepare statements and bind parameters
$stmt = $conn->prepare("INSERT INTO tbl_games (member_id, exercises, auth_by, auth_duration, student_id, instructor_id) VALUES (:member_id, :exercises, :auth_by, :auth_duration, :student_id, :instructor_id)");
$stmt->bindParam(':member_id', $member_id);
$stmt->bindParam(':exercises', $exercises);
$stmt->bindParam(':auth_by', $auth_by);
$stmt->bindParam(':auth_duration', $auth_duration);
$stmt->bindParam(':student_id', $student_id);
$stmt->bindParam(':instructor_id', $instructor_id);
// execute statements
$stmt->execute();
// success or error message
echo "New record created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
POST配列を多次元として使用する場合は、入力を配列として扱う必要があります。ここではそうであるように、私は 'name =" xxx [] "'さもなければ、そうしないでください。 –
これをデバッグするとき、挿入するIDの変数の値は何ですか?すべてのレコードが 'SELECT'クエリによって返されますか? *これらのクエリで使用している値は何ですか?これらの値に一致するレコードはどれですか? (さらに、3つの別々のテーブルに*非常に*類似したデータを格納し、最初はこれらの異なるクエリをすべて必要とするのは異常です。) – David
明らかに「コメント」ではなく「回答」のみに回答します。さて、私はここから出ています。私はこれを見て十分な時間を過ごした。 –