我有一个表单,用户可以输入成员,学生和/或讲师的姓名(姓和名)。他们还输入与这个问题无关的其他数据。这全部插入名为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数组用作多维,则需要将输入视为数组。 I.e .:'name =“xxx []”'这里似乎就是这种情况。否则,不要。 –
当你调试这个时,你插入的ID的变量值是多少?是否有任何由'SELECT'查询返回的记录?你在这些*查询中使用的值是什么,并且任何记录都完全匹配那些值*? (另外,在三个独立表中存储非常相似的数据似乎有点奇怪,并且首先要求所有这些不同的查询。) – David
显然只回答“回答”而不是“评论”。好吧,我离开这里。我花了足够的时间看这个。 –