PHP MySQL PDO->;在插入时,如果值已存在,则执行另一个查询
我有一个mysql数据库,有两个表。第一个表称为“uniqueReferences”,第二个表称为“duplicatedReferences”。这两个表只有两个字段:一个id字段(自动递增)和一个名为Reference的字段。我想要的是如下。尝试在“uniqueReferences”表中插入引用时,如果该引用已存在,则不要将其插入该表中,而是插入表“duplicatedReferences”中 因此,我尝试过但没有成功的方法如下。PHP MySQL PDO->;在插入时,如果值已存在,则执行另一个查询,php,mysql,pdo,Php,Mysql,Pdo,我有一个mysql数据库,有两个表。第一个表称为“uniqueReferences”,第二个表称为“duplicatedReferences”。这两个表只有两个字段:一个id字段(自动递增)和一个名为Reference的字段。我想要的是如下。尝试在“uniqueReferences”表中插入引用时,如果该引用已存在,则不要将其插入该表中,而是插入表“duplicatedReferences”中 因此,我尝试过但没有成功的方法如下。 1->将我的“uniqueReferences”表的字段引用设置
1->将我的“uniqueReferences”表的字段引用设置为“unique”。
2->进行以下操作
try{
$req = $prepared_insertQry_toUniqueRefTable -> execute(array(something));
if($req == 0){
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
}
}
catch(PDOException $e){echo $e->getMessage();}
不幸的是,这不起作用。我有以下错误
SQLSTATE[23000]:完整性约束冲突:1062重复条目
。希望有人能帮忙。干杯Marc根据您收到的消息,您似乎正在捕获一个异常,该异常的消息是。。。1062重复条目
。这意味着“uniqueReferences”表上的表约束将不允许重复条目。可能是因为该表中有一个主键。这是一件好事,它将使这个问题更容易解决
因此,如果每次尝试插入重复项时都会引发异常,那么我们就知道要插入到“duplicatedReferences”表中。您只需要验证引发的异常是否是由于重复条目引起的
试试这个:
try
{
$prepared_insertQry_toUniqueRefTable->execute(array(something));
}
catch (PDOException $e)
{
if ($e->getCode() == 1062)
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
else
echo $e->getMessage();
}
根据您收到的消息,您似乎正在捕获一个PDO异常,其消息是
。。。1062重复条目
。这意味着“uniqueReferences”表上的表约束将不允许重复条目。可能是因为该表中有一个主键。这是一件好事,它将使这个问题更容易解决
因此,如果每次尝试插入重复项时都会引发异常,那么我们就知道要插入到“duplicatedReferences”表中。您只需要验证引发的异常是否是由于重复条目引起的
试试这个:
try
{
$prepared_insertQry_toUniqueRefTable->execute(array(something));
}
catch (PDOException $e)
{
if ($e->getCode() == 1062)
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
else
echo $e->getMessage();
}
请参见与代码内联的我的注释:
try{
$req = $prepared_insertQry_toUniqueRefTable -> execute(array(something));
// this never executes because an Exception halts it here
/*if($req == 0){
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
}*/
}
catch(PDOException $e){
// this catch grabs the exception and executes the code within instead
// that code might log an error, echo the error message, or perform
// alternative logic. In your case you want to execute alterntative logic
// ie. your query
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
}
请参见与代码内联的我的注释:
try{
$req = $prepared_insertQry_toUniqueRefTable -> execute(array(something));
// this never executes because an Exception halts it here
/*if($req == 0){
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
}*/
}
catch(PDOException $e){
// this catch grabs the exception and executes the code within instead
// that code might log an error, echo the error message, or perform
// alternative logic. In your case you want to execute alterntative logic
// ie. your query
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
}
首先尝试插入数据,如果数据已经存在,则将其插入到表中以获得重复的行。由于该行存在时会生成异常,因此可以捕获该异常,然后将该行插入到复制表中:
try
{
$req = $prepared_insertQry_toUniqueRefTable -> execute(array(something));
}
catch (PDOException $e)
{
// you can use the value of errorInfo[1] to check the actual generated error code
if ($e->errorInfo[1] == 1062)
{
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
}
else
{
// rethrow the exception as we're not handling it
throw $e;
}
}
您可能需要对其进行一些调整以获得所需的内容,但这应该是它的要点。首先尝试插入数据,然后如果数据已经存在,则将其插入表中以获得重复的行。由于该行存在时会生成异常,因此可以捕获该异常,然后将该行插入到复制表中:
try
{
$req = $prepared_insertQry_toUniqueRefTable -> execute(array(something));
}
catch (PDOException $e)
{
// you can use the value of errorInfo[1] to check the actual generated error code
if ($e->errorInfo[1] == 1062)
{
$prepared_insertQry_toDuplicateRefTable->execute(array(something));
}
else
{
// rethrow the exception as we're not handling it
throw $e;
}
}
您可能需要对其进行一些调整以获得所需的内容,但这应该是它的要点。您是否可以将您的
$prepared\u insertQry\u toDuplicateRefTable…
放在catch
块中(在确保异常为“重复条目”错误之后)?Hello Travesty:)Thks以获取帮助。我是pdo的新手,认为catch块只是用来获取异常的。因此,我并没有真正理解您的建议…catch定义了当您遇到异常时要采取的操作这可能是对错误消息做了一些操作,或者在这种情况下,您希望它执行不同的查询。难道您不能将您的$prepared\u insertQry\u放入duplicatereffable…
块中的吗(确认异常为“重复输入”错误后)?Hello Travesty:)感谢您的帮助。我是pdo的新手,认为catch块只是用来获取异常的。因此,我没有真正理解您的建议…捕获定义了当您遇到异常时要采取的操作这可能是对错误消息做了一些操作,或者在这种情况下,您希望它执行不同的query.Thks。只是尝试一下。。。给我一点时间,我会回来的。只是尝试一下。。。等我一下,我会回复的