doulu4534 2018-06-03 11:40
浏览 54
已采纳

使用下拉列表将文本插入数据库

I have table in database that has 3 column, the first column id(PK), second column nameOfPerson and third column parent(foriegn key). When I entered name into text field and choose parent from drop down I want to insert into the DB the name under nameOfPerson and parent under parent. Why in my code when I clicked submit nothing happens?

This is my table

id  nameOfPerson    parent
3   John             NULL
4   Michel            3
5   Husam             4
6   Khalaf            5
7   Mark              5
---------------------------- 

this is my function to get the who can be parent

    public function displayParent(){
     //$statment = $this->db->prepare("SELECT DISTINCT  person.nameOfPerson, b.nameOfPerson as name FROM person LEFT JOIN person b ON (person.parent = b.id)");
        $statment = $this->db->prepare("SELECT id, nameOfPerson FROM person");
        $statment->execute();
        $result = $statment->fetchAll();
        foreach($result as $output){
            echo "<option>" .$output['nameOfPerson']."</option>";
        }
 }

this is my function to insert data into DB

    public function enterChild(){

        $statment = $this->db->prepare("INSERT INTO person (nameOfPerson, parent) VALUES(:name, :parent)");
        $statment->bindParam(':name',$_POST['name']);
        $statment->bindParam(':parent',$_POST['parent']);
        $statment->execute();
        $result = $statment->rowCount();
        if($result == "1")
        {
            $message = '<label>successfully</label>';
        }
            else
            {
                $message = '<label>Wrong</label>';
            }
    echo $message;
 }

and this is mu index code

<?php include_once('Family.php');
$object = new Family();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Family Tree</title>
</head>
<body>
<form method="post">
  Child Name: <input type ='text' name ='name' placeholder="Enter name here">
  <select name="parent" id="names" onchange="getSelectValue()">
  <option>--Select Parent--</option>
  <?php echo $object->displayParent() ?>
  </select>
    <br>
    <input type="submit" name="submit" value="Enter">
</form>

<script>
    //Get selected nema
    function getSelectValue(){

        var selectedValue = document.getElementById("names").value;
        console.log(selectedValue);
    }
</script>

<?php
    $object->GetFamilyTree();
    if(isset($_POST['submit'])){

        $name=  $_POST["name"];
        $parent= $_POST["parent"];
        $object->enterChild();
    }
?>


</body>
</html>
  • 写回答

1条回答 默认 最新

  • duanliusong6395 2018-06-03 12:17
    关注

    Your query fails since the column parent needs to be an interger while you're sending in a string.

    The issue is how you're outputting the selectbox for the parents.

    echo "<option>" .$output['nameOfPerson']."</option>";
    

    If you omit the value-attribute, the text (in this case, the name) will be sent instead.

    Add the id as value and it should work:

    echo "<option value='{$outout['id']}'>" .$output['nameOfPerson']."</option>";
    

    Now the id will be sent instead of the name.

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?