Php 在数据库中插入带有下拉列表的文本

Php 在数据库中插入带有下拉列表的文本,php,mysql,oop,pdo,Php,Mysql,Oop,Pdo,我在数据库中有一个表,它有3列,第一列idPK,第二列Person和第三列ParentForeign键。当我在文本字段中输入名称并从下拉列表中选择parent时,我想在DB中插入nameOfPerson下的名称和parent下的parent。为什么在我的代码中单击submit时什么都没有发生 这是我的桌子 id nameOfPerson parent 3 John NULL 4 Michel 3 5 Husam

我在数据库中有一个表,它有3列,第一列idPK,第二列Person和第三列ParentForeign键。当我在文本字段中输入名称并从下拉列表中选择parent时,我想在DB中插入nameOfPerson下的名称和parent下的parent。为什么在我的代码中单击submit时什么都没有发生

这是我的桌子

id  nameOfPerson    parent
3   John             NULL
4   Michel            3
5   Husam             4
6   Khalaf            5
7   Mark              5
---------------------------- 
这是我的功能,以获得谁可以是家长

    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>";
        }
 }
这是我将数据插入数据库的函数

    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;
 }
这是mu索引代码

<?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>

您的查询失败,因为在发送字符串时,列父项必须是整数

问题是如何为家长输出选择框

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

现在,将发送id而不是名称。

您是否从enterChild方法收到任何消息?你做过调试吗?检查代码中不同阶段的值以查看您得到了什么以及是否调用了该方法等?您还应该为查询添加一些正确的错误处理:是的,他得到了该方法并回显错误@Magnuseriksson,然后添加错误处理我的第二个链接,以了解查询是否因某种原因失败。现在它的工作。。。非常感谢,先生。非常感谢,马格纳斯先生
echo "<option value='{$outout['id']}'>" .$output['nameOfPerson']."</option>";