2016-11-20 98 views
1

我有一個表單,用戶可以輸入成員,學生和/或講師的姓名(姓和名)。他們還輸入與這個問題無關的其他數據。這全部插入名爲games的表格中。名爲members,studentsinstructors的表格已經存在,其中的字段包括id,first_namelast_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; 

?> 
+0

如果您想要將POST數組用作多維,則需要將輸入視爲數組。 I.e .:'name =「xxx []」'這裏似乎就是這種情況。否則,不要。 –

+0

當你調試這個時,你插入的ID的變量值是多少?是否有任何由'SELECT'查詢返回的記錄?你在這些*查詢中使用的值是什麼,並且任何記錄都完全匹配那些值*? (另外,在三個獨立表中存儲非常相似的數據似乎有點奇怪,並且首先要求所有這些不同的查詢。) – David

+0

顯然只回答「回答」而不是「評論」。好吧,我離開這裏。我花了足夠的時間看這個。 –

回答

1

您可以輸入一個不存在的,必須進行驗證的成員。

或者先加載成員加載組合,然後用戶從組合中選擇id併發送給服務器id成員。

+0

我知道一個組合框會容易得多,但是對於動態和大量的成員/學生/教師是否可行? – sinesine