dqt20140129 2015-03-31 01:50
浏览 57
已采纳

SQL PHP可搜索表单字段添加到新数据库

How do I alter the following code to allow me to extract data from another table (data2), and post it as I did the others(name,position,bio). Basically I want another form field that I can search from to find an item from another table, and add into this one.

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>
  • 写回答

1条回答 默认 最新

  • doubi8965 2015-03-31 01:58
    关注

    Well you could have in the table that the form submits to a location_id that is a key linked to the locations table.

    Then all you need to do is do an sql query where you select values using LIKE (see MySQL LIKE - I think w3schools covers this well)

    You could call this function in Ajax to check every key press.

    Alternatively load all the locations to a hidden element and when the input is focused they appear and as you type you use JavaScript to hide those that don't match.

    I would write an example but I'm on my phone. Hope this helps a little.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看