php - 为什么我的“添加”按钮还会添加一个空对象
问题描述
所以我的添加按钮有效,但有时它也会添加一个空对象,但并非总是如此。所以这是我在 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);
}
解决方案
你没有添加任何空值检查
if(!empty($this->name) && !empty($this->name))
{
// your code
}
推荐阅读
- typescript - 如何在 VS 代码中禁用保存时自动加入行?
- python - 解析 XML 时出错:列表索引必须是整数或切片,而不是 Str
- powershell - 当显示名称中有特殊字符时,获取通讯组中所有显示名称的 SMTP 地址
- json - 如何正确解析和输入 graphql 结果?
- puppet - 使用 puppet 迭代一定次数
- three.js - 三个 JS 中的动画透明度
- javascript - 遍历 CSV 文件并为每一行运行等待?
- rest - 使用 Vertx 实现客户端分页 API 调用的最佳方式是什么?
- sql - 在 SQL Server 中以十六进制存储数据的充分理由是什么?
- sqlite3-ruby - Ruby 新手,sqlite3 问题。我已经尝试了一切,但我仍然卡住了