douping3860 2013-03-07 14:36
浏览 41

需要帮助将我的表单数据提交到数据库中

OK, let me start off by saying im an amateur, so all your help is really appreciated.

OK, I have a form, called 'skills.php' and on this form, you enter the following fields 'Skills_ID, Employee_ID, First_name, Last_name and Skill'. I have used java so when you select employee_ID, the name fields change to what employee that is (linked to employee table).

HOWEVER, since i have added this function, i can not save my form data into my database. Maby i accidently deleted a line of code when implementing the java function. Can someone help me figure it out? below is my form 'Skill.php':

<html>

<?php
// Connecting to database
$pdo = new PDO("mysql:host=localhost;dbname=hrmwaitrose;charset=utf8", "root", "");
?>
<html>
<head>
        <link type="text/css" rel="stylesheet" href="style.css"/>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <!-- Online Jquery -->
        <title>Skill</title>
    </head>

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

            <form action="insertskill.php" method="post">
                <div>
                    <p>
                        Skill ID:
                        <input type="text" name="Training_ID">
                    </p>
                    <p>
                        Employee ID:
                        <select id="Employee_ID">
                            <option value="">Select one</option>
                            <?php
                            $st = $pdo->prepare("SELECT Employee_ID FROM Employee");
                            $st->execute();
                            $rows = $st->fetchAll(PDO::FETCH_ASSOC);
                            foreach ($rows as $row) {
                                ?><option value="<?php echo $row ['Employee_ID']; ?>"><?php echo $row ['Employee_ID']; ?></option><?php
                            }
                        ?>
                        </select>
                    <p>
                        First name:
                        <input type="text" name="First_name" id="First_name">
                    </p>
                    <p>
                        Last name:
                        <input type="text" name="Last_name" id="Last_name">
                    </p>
                    <p>
<p>Skill: <select name="Skill">
  <option value="">Select...</option>
  <option value="Checkouts">Checkouts</option>
  <option value="Fresh goods">Fresh goods</option>
  <option value="Dry goods">Dry goods</option>
  <option value="Fruit & Veg">Fruit & Veg</option>
  <option value="Operations">Operations</option>
</select>
</p>
                    <input type="submit">
                    <INPUT Type="BUTTON" VALUE="Back" ONCLICK="window.location.href='index.html'">
            </form>
        </div>
    <script type="text/javascript">
        $(function() { // This code will be executed when DOM is ready
            $('#Employee_ID').change(function() { 
                var $self = $(this); // jQuery object with the select inside
                $.post("insertskill.php", { Employee_ID : $self.val()}, function(json) {
                    if (json && json.status) {
                        $('#First_name').val(json.name);
                        $('#Last_name').val(json.lastname);
                    }
                })
            });
        })
    </script>
    </body>
</html>

And here is the code used when the submit button is pressed 'insertskill.php':

<?php
$pdo = new PDO("mysql:host=localhost;dbname=hrmwaitrose;charset=utf8", "root", "");

header("Content-Type:application/json; Charset=utf-8");


$st = $pdo->prepare("SELECT First_name, Last_name FROM Employee WHERE Employee_ID = :employee_id");
$st->execute(array ('employee_id' => $_POST['Employee_ID']));
$data = $st->fetch(PDO::FETCH_ASSOC);

echo json_encode(array ('status' => true, 'name' => $data ['First_name'], 'lastname' => $data ['Last_name']));
?>

Just by looking at this code imn pretty sure i might of accidently deleted the coding to insert it into database, HOWEVER i dont know how to fix it :( can someone help? much appreciated! Thanks in advance

  • 写回答

3条回答 默认 最新

  • dongzhijing8202 2013-03-07 14:53
    关注

    in "insertskill.php" line 6:

    $st = $pdo->prepare("SELECT First_name, Last_name FROM Employee WHERE Employee_ID = :employee_id");
    

    You're performing a SELECT query (read), sounds to me more like you wanted an INSERT query (write).

    An INSERT statement in your case would be something similar to:

    $st = $pdo->prepare("INSERT INTO Employee (Training_Id,Employee_Id,First_Name,Last_Name,Skill) VALUES (:training_id,:employee_id,:first_name,:last_name,:skill)")
    
    $st->execute(array(':training_id' => $training_id, ':employee_id' => ...));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行