首页 > 解决方案 > 为什么我的“添加”按钮还会添加一个空对象

问题描述

所以我的添加按钮有效,但有时它也会添加一个空对象,但并非总是如此。所以这是我在 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;

}

这是部门服务中的一个:

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

标签: phpangularapi

解决方案


你没有添加任何空值检查

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

推荐阅读