php中的现有记录
每当我在UI中添加新的位置/描述时,它都会自动保存到我的数据库中。我的问题是,我仍然可以添加一个地方,即使它已经存在。我所希望的是,如果新的描述/位置已经存在,它将不会保存/更新 任何建议都将不胜感激。 谢谢 这是我的编辑位置:php中的现有记录,php,postgresql,Php,Postgresql,每当我在UI中添加新的位置/描述时,它都会自动保存到我的数据库中。我的问题是,我仍然可以添加一个地方,即使它已经存在。我所希望的是,如果新的描述/位置已经存在,它将不会保存/更新 任何建议都将不胜感激。 谢谢 这是我的编辑位置: public function executeEditPlaces(sfWebRequest $request) { try{ $id = pg_escape_string(strip_tags($request->getParameter("id")));
public function executeEditPlaces(sfWebRequest $request)
{
try{
$id = pg_escape_string(strip_tags($request->getParameter("id")));
// $id = $request->getParameter("id");
$query = "select description from country.regions where id = ('$id')";
// die($query);
$result=$this->conn->fetchAll($query);
if(count($result)>0){
$v = $result[0];
$data['data'] = array('description' => $this->formatString($v['description']));
}
$data['success']=true;
$data['msg']=$msg;
die(json_encode($data));
}
catch(exception $e)
{
$data['success']=false;
$data['msg']=$e->getMessage();
die(json_encode($data));
}
}
这是我的更新位置:
public function executeUpdatePlaces(sfWebRequest $request)
{
try{
$by = $_SESSION['employee_id'];
$now = date("Y-m-d H:I:s");
$id = $request->getParameter("id");
$description = pg_escape_string(strip_tags($request->getParameter("description")));
$description = trim($description);
if(strlen($description) == 0)
{
die('cannot be empty');
}
else
{
$query = "update country.regions set description=('$description'),modified_by=('$by'),date_modified=('$now') where id=('$id')";
$msg = "Existing Region Successfully Updated.";
$this->conn->execute($query);
$data['success']=true;
$data['msg']=$msg;
die(json_encode($data));
}
}
catch(exception $e)
{
$data['success']=false;
$data['msg']=$e->getMessage();
die(json_encode($data));
}
}
有两种方法:
$query = "select description from country.regions";
$result=$this->conn->fetchAll($query);
if(count($result)>0){
print 'already exist'
} else {
//insert code
}
$query = "select description from country.regions";
$result=$this->conn->fetchAll($query);
if(count($result)>0){
print 'already exist'
} else {
//insert code
}
$query = "select description from country.regions";
$result=$this->conn->fetchAll($query);
if(count($result)>0){
print 'already exist'
} else {
//insert code
}
$query = "select description from country.regions";
$result=$this->conn->fetchAll($query);
if(count($result)>0){
print 'already exist'
} else {
//insert code
}
请检查重复条目的
$description
是否确实相同
对于必须唯一的列,您可以在SQL模式中引入UNIQUE
约束-它将在较低的层中引入安全性
请检查说明是否为bytea类型
bytea
。如果是,应使用pg_escape_bytea
而不是pg_escape_string
请检查重复条目的$description
是否确实相同
对于必须唯一的列,您可以在SQL模式中引入UNIQUE
约束-它将在较低的层中引入安全性
请检查说明是否为bytea类型
bytea
。如果是,应使用pg_escape_bytea
而不是pg_escape_string
请检查重复条目的$description
是否确实相同
对于必须唯一的列,您可以在SQL模式中引入UNIQUE
约束-它将在较低的层中引入安全性
请检查说明是否为bytea类型
bytea
。如果是,应使用pg_escape_bytea
而不是pg_escape_string
请检查重复条目的$description
是否确实相同
对于必须唯一的列,您可以在SQL模式中引入UNIQUE
约束-它将在较低的层中引入安全性
请检查说明是否为bytea类型
bytea
。如果是,则应使用pg_escape_bytea
而不是pg_escape_string
使用unique
约束更改数据库架构。是的,只需将描述/位置定义为唯一索引。@davidkonrad我必须在哪里定义唯一索引?@Mikel,请参阅下面的user1168095答案。我建议您使用PHPMyadmin这样的工具,创建索引非常容易。使用unique
约束更改您的DB模式。是的,只需将description/place定义为唯一索引即可。@davidkonrad我必须在哪里定义唯一索引?@Mikel,请参阅下面的user1168095答案。我建议您使用PHPMyadmin这样的工具,创建索引非常容易。使用unique
约束更改您的DB模式。是的,只需将description/place定义为唯一索引即可。@davidkonrad我必须在哪里定义唯一索引?@Mikel,请参阅下面的user1168095答案。我建议您使用PHPMyadmin这样的工具,创建索引非常容易。使用unique
约束更改您的DB模式。是的,只需将description/place定义为唯一索引即可。@davidkonrad我必须在哪里定义唯一索引?@Mikel,请参阅下面的user1168095答案。我建议您使用PHPMyadmin这样的工具,在这里创建索引非常容易。