为什么我的“添加”按钮还会添加一个空对象

时间:2018-10-10 10:56:39

标签: php angular api

因此,我的添加按钮可以工作,但有时也会添加一个空对象,尽管并非总是如此。 这是我在API中的代码:

function create(){

    // query to insert record
    $query = "INSERT INTO
                " . $this->table_name . "
            SET
                name=:name,location=:location";

                echo $query;

    // prepare query
    $stmt = $this->conn->prepare($query);
    if (!$stmt)
      var_dump($this->conn->errorInfo());

    // sanitize
    $this->name=htmlspecialchars(strip_tags($this->name));
    $this->location=htmlspecialchars(strip_tags($this->location));



    // bind values
    $stmt->bindParam(":name", $this->name);
    $stmt->bindParam(":location", $this->location);



    // execute query
    if($stmt->execute()){
        return true;
    }
var_dump($this->conn->errorInfo());
    return false;

}

这是DepartmentService中的一个:

  addDepartment (name:string, location:string):Observable<any> {
      return this.http.post(this.depCreate,{
            "name": name,
            "location": location},
            httpOptions);
    }

2 个答案:

答案 0 :(得分:0)

u没有添加任何对空值的检查

if(!empty($this->name) && !empty($this->name))
{
// your code
}

答案 1 :(得分:0)

更改此

 return this.http.post(this.depCreate,{
        "name": name,
        "location": location},
        httpOptions);
}

 return this.http.post(this.depCreate,{
        name,
        location},
        httpOptions);
}