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 =");
        $statment = $this->db->prepare("SELECT id, nameOfPerson FROM person");
        $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)");
        $result = $statment->rowCount();
        if($result == "1")
            $message = '<label>successfully</label>';
                $message = '<label>Wrong</label>';
    echo $message;

and this is mu index code

<?php include_once('Family.php');
$object = new Family();
<title>Family Tree</title>
<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() ?>
    <input type="submit" name="submit" value="Enter">

    //Get selected nema
    function getSelectValue(){

        var selectedValue = document.getElementById("names").value;


        $name=  $_POST["name"];
        $parent= $_POST["parent"];

  • 写回答

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.

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



  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 串口发送数据和接收数据
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作
  • ¥15 做过TCL海信电视小米电视相关影视会员软件私我
  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示