2015-03-31 53 views
0

如何更改以下代碼以允許我從其他表格(data2)中提取數據,並將其作爲其他人員(姓名,職位,生物)進行發佈。基本上我想要另一個表單域,我可以從另一個表中找到一個項目,然後添加到這個項目中。SQL PHP可搜索表格字段添加到新數據庫

Example Image

<?php 
require 'db/connect.php'; 

$error  = ""; //variable to hold our form error message 
$success = ""; //variable to hold our success message 

if(isset($_POST['create'])){ 
$name  = trim($_POST['name']); 
$position = trim($_POST['position']); 
$bio  = trim($_POST['bio']); 

if(empty($name) && empty($position) && empty($bio)){ 
    $error = "You must fill all fields."; 
}else{ 
    $insert = $db->prepare("INSERT INTO staff (name, position, bio,  joined) VALUES (?, ?, ?, NOW())"); 
    $insert->bind_param(sss, $name, $position, $bio); 
    if($insert->execute()){ 
     //$success = "Staff added successfully!"; 
     header("location:index.php"); 
    } 

} 

} 
?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <link rel="stylesheet" href="styles.css"> 
</head> 
<body> 
    <div id="wrapper"> 
     <h1>Create New Staff</h1> 
     <span class="error"><?php if(isset($error)) echo $error;?></span> 
     <span class="success"><?php if(isset($success)) echo   $success;?>  </span> 
     <form action="" method="post"> 
      <table class="table"> 
       <tr> 
        <td><label for="name">Name:</label></td> 
        <td><input type="text" id="name" name="name"></td> 
       </tr> 
       <tr> 
        <td><label for="position">Position:</label></td> 
        <td><input type="text" id="position" name="position"></td> 
       </tr>    
       <tr> 
        <td><label for="bio">Bio:</label></td> 
        <td><textarea id="bio" name="bio"></textarea></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td><button type="submit" class="create"  name="create">CREATE</button>&nbsp;&nbsp;&nbsp;&nbsp;<a class="btn"  href="index.php">BACK</a></td> 
       </tr> 
      </table> 
     </form> 
    </div> 
</body> 
</html> 
+0

您可以使用jQuery的自動完成功能。或者你可以參考這裏:http://stackoverflow.com/questions/21298169/autocomplete-textbox-results-based-from-sql-database – 2015-03-31 02:06:43

+0

請你可以裁剪你的形象到必要的部分?因爲它使內容更長,沒有一個合理的原因 – dvhh 2015-03-31 03:26:21

回答

0

那麼你可以在該表單提交到LOCATION_ID鏈接到的位置表鍵的表。

然後,所有你需要做的是您選擇使用LIKE值做一個SQL查詢(見MySQL的LIKE - 我認爲W3Schools的佔地面積這口井)

你可以調用這個函數在阿賈克斯檢查每一個按鍵。

或者將所有位置加載到隱藏元素,並在輸入焦點時顯示,並且在鍵入時使用JavaScript來隱藏那些不匹配的元素。

我會寫一個例子,但我在我的手機上。希望這有所幫助。

+0

恐怕我是一個總編程的新手。我越來越多地使用php和sql進行學習,但對於ajax沒有經驗。有沒有人有這個紳士正在談論的代碼樣本? – Steven 2015-04-01 15:59:44