John Hebert Trindade Posted December 5, 2023 at 12:30 PM Report #632319 Posted December 5, 2023 at 12:30 PM Bom dia, Tenho uma função para validar se os documentos já foram cadastrado com outro técnico. Mas preciso verificar o tipo do documento e o número do documento e ignorando o proprio id. Mas preciso que tudo seja verdadeiro, mas infelizmente não devolve resultado. O que errei? public function validateDocuments(int $tipodoc, string $docIdentify, string $paisnif, string $nif, bool|null $edit = null, int|null $id = null): void { $this->tipodoc = $tipodoc; $this->docIdentify = $docIdentify; $this->paisnif = $paisnif; $this->nif = $nif; $this->edit = $edit; $this->id = $id; $verifyDocuments = new \App\adms\helpers\AdmsRead(); if (($this->edit == true) and (!empty($this->id))) { $verifyDocuments->fullRead("SELECT id FROM adms_tecnicos WHERE (tipodoc=:tipodoc AND numero_doc=:numero_doc AND paisnif=:paisnif AND nif=:nif AND id <>:id) LIMIT :limit", "tipodoc={$this->tipodoc}&numero_doc={$this->docIdentify}&paisnif={$this->paisnif}&nif={$this->nif}&id={$this->id}&limit=1"); } else { $verifyDocuments->fullRead("SELECT id FROM adms_tecnicos WHERE id =:id LIMIT :limit", "id={$this->id}&limit=1"); } $this->resultBd = $verifyDocuments->getResult(); if (!$this->resultBd) { $this->result = true; } else { $this->utils->setSessionMsg('Erro: Técnico já cadastrado, por favor verifique!', 'danger'); $this->result = false; } }
Ivo Vicente Posted December 5, 2023 at 02:42 PM Report #632320 Posted December 5, 2023 at 02:42 PM Quando executa o script, entra no if que pretendes? Já tentaste executar o query diretamente no SQL sem ser via PHP? Ou ver a query string que é gerada pelo PHP e executar diretamente? Ás vezes pode ser um detalhe na query que não recebe uma variável, ou mesmo um typo visto que o método fullRead é só strings, existem margem para erro. Se nada resultar, avisa. Feito é melhor que perfeito
John Hebert Trindade Posted December 5, 2023 at 02:55 PM Author Report #632322 Posted December 5, 2023 at 02:55 PM Este é o metodo fullRead public function fullRead(string $query, string|null $parseString = null): void { $this->select = $query; if (!empty($parseString)) { $pairs = explode('&', $parseString); foreach ($pairs as $pair) { list($key, $value) = explode('=', $pair); $this->values[$key] = $value; } } $this->exeInstruction(); } Se eu coloco somente um campo dá certo, seja o numero do documento ou o tipo. Exemplo: $verifyDocuments->fullRead("SELECT id FROM adms_tecnicos WHERE numero_doc=:numero_doc AND id <>:id) LIMIT :limit", "numero_doc={$this->docIdentify}&id={$this->id}&limit=1");
John Hebert Trindade Posted December 5, 2023 at 05:18 PM Author Report #632323 Posted December 5, 2023 at 05:18 PM @Ivo Vicente tens toda a razão, quando uso $paisnif que é uma chave estrangeira, ou seja, um inteiro não resulta, mas quando uso apenas um string funciona.
John Hebert Trindade Posted December 5, 2023 at 06:32 PM Author Report #632326 Posted December 5, 2023 at 06:32 PM (edited) Fiz a mesma pesquisa directo no sql e deu certo, como eu transformo-a da forma que preciso? SELECT * FROM `adms_tecnicos` WHERE `id` != 2 AND `adms_tipo_doc_id` = 3 AND `numero_doc` LIKE '82Q8J1A23B2' AND `nif_pais` LIKE 'PT' AND `numero_nif` LIKE '234.901.477' ORDER BY `adms_nacionalidade_id` ASC Edited December 5, 2023 at 06:32 PM by John Hebert Trindade
Ivo Vicente Posted December 6, 2023 at 11:11 AM Report #632330 Posted December 6, 2023 at 11:11 AM Então, se tens a query que precisas em SQL, agora é passar para a query string no PHP com as variáveis necessárias (visto que algumas não chegam do modo correto ex: $paisnif) Feito é melhor que perfeito
John Hebert Trindade Posted December 6, 2023 at 01:18 PM Author Report #632332 Posted December 6, 2023 at 01:18 PM Deixo aqui a forma correcta do select, o restante esta a funcionar correctamente. "SELECT id FROM adms_tecnicos WHERE (adms_tipo_doc_id=:tipodoc AND numero_doc=:numero_doc) OR (nif_pais=:paisnif AND numero_nif=:nif) AND id <>:id LIMIT :limit", "tipodoc={$this->tipodoc}&numero_doc={$this->docIdentify}&paisnif={$this->paisnif}&nif={$this->nif}&id={$this->id}&limit=1" );
Ivo Vicente Posted December 6, 2023 at 04:42 PM Report #632335 Posted December 6, 2023 at 04:42 PM Se já tens a string SQL no PHP com os placeholder no lugar, ficou a faltar alguma coisa? Dá erro? Continua a não dar resultados? Feito é melhor que perfeito
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now