2013-02-25 69 views
0

我有兩個表,這些如下:從存儲在數據庫中的表中檢索並顯示數據到表單中(使用外鍵)。

僱員 - (EMPLOYEE_ID,名字,姓氏,地址等) 培訓 - (training_ID,的Employee_ID,名字,姓氏,訓練)

僱員ID是外鍵在訓練表中。

我有一張表格供培訓表使用,這個表格可以讓那些會進入哪個員工需要哪種培訓的人使用。

無論如何,我的培訓表單上,我可以有一個僱員字段的下拉框,它會自動更新該僱員ID的名字和姓氏?

或者只是無論如何我可以有我的表格,這樣員工ID總是有正確的名字和姓氏。

這裏是我的表單(html)和php代碼提交給數據庫。

<html> 
<head> 
<link type="text/css" rel="stylesheet" href="style.css"/> 
<title>Training</title> 
</head> 

<body> 
<div id="content"> 
<h1 align="center">Add Training</h1> 

<form action="inserttraining.php" method="post"> 
<div> 
<p>Training ID: <input type="text" name="Training_ID"></p> 
<p>Employee ID: <input type="text" name="Employee_ID"></p> 
<p>First name: <input type="text" name="First_name"></p> 
<p>Last name: <input type="text" name="Last_name"></p> 
<p> 
Training required? 
<select name="Training"> 
<option value="">Select...</option> 
<option value="Customer Service">Customer Service</option> 
<option value="Bailer">Bailer</option> 
<option value="Reception">Reception</option> 
<option value="Fish & meat counters">Fish & meat counters</option> 
    <option value="Cheese counters">Cheese counters</option> 
</select> 
</p> 
<input type="submit"> 
</form> 
</div> 

</body> 
</html> 

和PHP代碼:

<?php 
$con = mysql_connect("localhost","root",""); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("hrmwaitrose", $con); 

$sql="INSERT INTO training (Training_ID, Employee_ID, First_name, Last_name, Training) 
VALUES 
    ('$_POST[Training_ID]','$_POST[Employee_ID]','$_POST[First_name]','$_POST[Last_name]','$_POST[Training]')"; 


if (!mysql_query($sql,$con)) 
    { 
    die('Error: ' . mysql_error()); 
    } 
echo "1 record added"; 

mysql_close($con); 
?> 

在此先感謝和抱歉,如果我的代碼犯規顯示效果出色。

回答

0

如果您想使用下拉菜單更新您的文本字段(名字,姓氏),您將需要使用ajax。

只需使用下拉列表的onChange事件創建一個ajax請求,並使用javascript將返回的值設置爲文本字段。

對於預填充值,你可以做到以下幾點:

//code to get data from database 

//store the result in an array 
$result = mysql_fetch_array($sql); 

//change the html in your form 
<p>First name: <input type="text" name="First_name" value="<?php if(!empty($result['First_name'])) echo $result['First_name']; ?>"></p> 
<p>Last name: <input type="text" name="Last_name" value="<?php if(!empty($result['last_name'])) echo $result['last_name']; ?>"></p> 
+0

感謝你的幫助,雖然我不知道如何有一個下拉從僱員表包含僱員ID值框。無論如何要做到這一點? – user2108411 2013-02-25 20:05:41

+0

閱讀鏈接中的教程... http://www.c-sharpcorner.com/UploadFile/051e29/dropdown-list-in-php/ – 2013-02-25 20:51:36

相關問題