2016-11-19 67 views
3

我有兩個表,membersgames。在members是數據,如member_idfirst_namelast_namePHP的MySQL - 用戶輸入名稱和相應的ID被插入

我想要做的就是創建一個表單games,在這裏用戶可以輸入誰參加成員的姓氏和名字(在一個字符串,而不是單獨)和一些PHP代碼查詢這個名字,找到相應的ID並存儲它。當然,member_idgames的外鍵,但用戶不會知道該成員的ID,他們只會知道他們的名字。

如果有人可以解釋我將如何去做這件事,我將不勝感激。

形式:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Form</title> 
</head> 
<body> 
<form action="action.php" method="post"> 
    <p> 
     <label for="date">Date:</label> 
     <input type="date" name="date" id="date"> 
    </p> 
    <p> 
     <label for="duration">Duration:</label> 
     <input type="time" name="duration" id="duration"> 
    </p> 
     <p> 
     <label for="member_id">Member Name:</label> 
     <input type="text" name="member_id" id="member_id"> 
    </p> 
    <input type="submit" value="Submit"> 
</form> 
</body> 
</html> 

操作:

<?php 

// database connection 
include 'pdo_config.php'; 

try { 
// new pdo connection 
$conn = new PDO($dsn, $user, $pass, $opt); 

// prepare statement and bind parameters 
$stmt = $conn->prepare("INSERT INTO games (date, duration, member_id) 
    VALUES (:date, :duration, :member_id)"); 

$stmt->bindParam(':date', $date); 
$stmt->bindParam(':duration', $duration); 
$stmt->bindParam(':member_id', $member_id); 

// post data 
$date = $_POST['date']; 
$duration = $_POST['duration']; 
$member_id = $_POST['member_id']; 

// execute statement 
$stmt->execute(); 

// success or error message 
echo "New record created successfully"; 
} 
catch(PDOException $e) 
{ 
echo "Error: " . $e->getMessage(); 
} 

$conn = null; 

?> 

回答

3

這應該工作。

要求用戶在表單中輸入成員名稱而不是成員id。然後對數據庫進行第一次查詢以從成員名稱中獲取成員標識。

請記住從名稱中搜索成員ID並不是一個好主意,因爲您可以讓多個成員具有相同的名稱。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Form</title> 
</head> 
<body> 
<form action="action.php" method="post"> 
    <p> 
     <label for="date">Date:</label> 
     <input type="date" name="date" id="date"> 
    </p> 
    <p> 
     <label for="duration">Duration:</label> 
     <input type="time" name="duration" id="duration"> 
    </p> 
    <p> 
     <label for="member_name">Member Name:</label> 
     <input type="text" name="member_name" id="member_name"> 
    </p> 
    <input type="submit" value="Submit"> 
</form> 
</body> 
</html> 

<?php 

// database connection 
include 'pdo_config.php'; 

try { 
// new pdo connection 
$conn = new PDO($dsn, $user, $pass, $opt); 

// post data 
$date = $_POST['date']; 
$duration = $_POST['duration']; 

// Note that the explode only works well if user inputs one blank space to separate the name 
// You can try to improve the separation method or better use two different inputs in the form 

$nameArray = explode(" ", $_POST['member_name']); 
$first_name = $nameArray[0]; 
$last_name = $nameArray[1]; 

$statement = $conn->prepare("SELECT member_id FROM members WHERE first_name = :first_name AND last_name = :last_name"); 
$statement->execute(array(':fisrt_name' => $first_name, ':last_name' => $last_name)); 
$row = $statement->fetch(); 

$member_id = $row['member_id']; 


// prepare statement and bind parameters 
$stmt = $conn->prepare("INSERT INTO games (date, duration, member_id) 
    VALUES (:date, :duration, :member_id)"); 

$stmt->bindParam(':date', $date); 
$stmt->bindParam(':duration', $duration); 
$stmt->bindParam(':member_id', $member_id); 

// execute statement 
$stmt->execute(); 

// success or error message 
echo "New record created successfully"; 
} 
catch(PDOException $e) 
{ 
echo "Error: " . $e->getMessage(); 
} 

$conn = null; 

?> 
+0

@sinesine我不能嘗試的代碼,因爲我還沒有創建的數據庫。請讓我知道,如果它不適合你,我會嘗試修復它 – nanocv

+1

不用擔心,非常感謝你的答案。我現在要測試它,我會讓你知道發生了什麼。 – sinesine

+0

'「請注意,爆炸只適用於用戶輸入一個空格來分隔名稱」' 我不確定你在這裏是什麼意思,你是說用戶必須輸入名字第一個和最後一個名字,比如「John Smith」,它能夠正常工作?他們會用其他方式輸入一個名字嗎?像'JohnSmith'例如? – sinesine