检查db中是否存在两个值(php + mysql)

时间:2014-12-02 16:49:24

标签: php mysql

希望一切顺利。 我正面临一个挑战,经过阅读和搜索,我决定召唤众神。

我的数据库中有一个名为“tclinte”的表,其中包含 Names

|CliNIF | CliNome | CliNumProc | CliNIC | CliOrigem | CliGestor | CliContactoGC|    

CliNIF(主键)和CliNome(单独/不能重复,其余的重要事项)

因此,我想检查在向该表插入数据时,新的CliNIF或新的CliNome是否已经在表中,例如:

if (CliNIF and CliNome exists) { update all fields except them}    
else if (CliNIF and CliNome don't exist) { create all fields}    
else if (CliNIF exists and CliNome not exists) { update fields except CliNIF}    
else { update fields except CliNome}    

----------------我有这个,但它只检查CliNIF是否存在,尝试将EXACT与EXi相同,但我收到错误“mysql_fetch_array()期待资源布尔给出“即使它应该工作-------

N.º1检查nif是否存在 - 工作正常 N.º2 - 检查名称是否存在 - 给出错误)

$queryTCliente1 = "SELECT * FROM TCliente WHERE CliNIF =".$_POST['CliNIF'];
                $searchTCliente1 = mysql_query($queryTCliente1);
                $resultTCliente1 = mysql_fetch_array($searchTCliente1);
                if (empty($resultTCliente1)){ 
                    //PREENCHIMENTO TABELA CLIENTE
                    $insertTable= mysql_query("insert into TCliente (CliNIF, CliNome, CliNumProc, CliNIC, CliOrigem, CliGestor, CliContactoGC) 
                    values ( '".$_POST['CliNIF']."', '".$_POST['CliNome']."','".$_POST['CliNumProc']."', '".$_POST['CliNIC']."', 
                    '".$_POST['CliOrigem']."', '".$_POST['CliGestor']."', '".$_POST['CliContactoGC']."');");
                }      

$queryTCliente2 = "SELECT * FROM TCliente WHERE CliNome =".$_POST['CliNome'];
                $searchTCliente2 = mysql_query($queryTCliente2);
                $resultTCliente2 = mysql_fetch_array($searchTCliente2);
                if (empty($resultTCliente2)){ 
                    //PREENCHIMENTO TABELA CLIENTE
                    $insertTable= mysql_query("insert into TCliente (CliNIF, CliNome, CliNumProc, CliNIC, CliOrigem, CliGestor, CliContactoGC) 
                    values ( '".$_POST['CliNIF']."', '".$_POST['CliNome']."','".$_POST['CliNumProc']."', '".$_POST['CliNIC']."', 
                    '".$_POST['CliOrigem']."', '".$_POST['CliGestor']."', '".$_POST['CliContactoGC']."');");
                }    

1 个答案:

答案 0 :(得分:0)

而不是

 "SELECT * FROM TCliente WHERE CliNome =".$_POST['CliNome'];

请试试这个。希望这能解决您的错误

 "SELECT * FROM TCliente WHERE CliNome ='".$_POST['CliNome']."'";