UPDATE problem and WHERE clause with two conditions

1

After much research on the internet without a solution, I turn to the "college students" on duty! I have two tables: contract (primary key) and contacts. The second being linked to the first. I set the records of the first one and select change in a record. For each Agreement I can have multiple contacts.

The CRUD of the contract table is OK. The problem is in the Contact table. I created a page with iframe, to display the record of the contract table and their respective records in the contact table. Just to change the contact, I pass the Idcont (primary key of the contract table) plus the date information of the contact table. Clicking on change does nothing. Neither the error message nor the change. I have already reviewed all the code, I changed method from bindParam to bindValue, etc.

Follow the change form code.

<html>

<head>
  <meta charset="utf-8">
  <title>Cadastro de contrato</title>
</head>

<body>
  <Table align="center" cellpadding="5" cellspacing="5" border="1">
    <caption align="center" size="20px">
      <font size="5px">Alterar Contato</font>
    </caption>
    <form name="form1" action="altera_cont.php" method="post">
      <tr>
        <td>
          <input type="hidden" name="idcont" id="fixo" size="5" value="<?=$resultado['idcont'] ?>"> Data: <input type="date" name="cdata" id="cdata" size="10" readonly="true" value="<?=date('d/m/Y',strtotime($resultado['cdata'])) ?>">&nbsp; Contato:
          <input
            type="text" name="contato" id="contato" size="50" value="<?=$resultado['contato'] ?>"> &nbsp; Obs.: <input type="text" name="obs" id="obs" size="85" value="<?=$resultado['obs'] ?>">&nbsp;
            <input type="submit" value="Alterar"></td>
      </tr>

    </form>
  </table>
</body>

</html>
The change routine is this:

<?php
require_once 'inicia.php';
/** COLETA AS INFORMAÇÕES DIGITADAS NO FORMULÁRIO FORM_ALTERA.PHP **/
$idcont = isset($_POST['idcont']) ? $_POST['idcont'] : null;
$cdata = isset($_POST['cdata']) ? $_POST['cdata'] : null;
$cdata = date('Y-m-d',strtotime($cdata));
$contato = isset($_POST['contato']) ? $_POST['contato'] : null;
$obs = isset($_POST['obs']) ? $_POST['obs'] : null;
/** VERIFICA SE TODOS OS CAMPOS DO FORMULÁRIO ESTÃO PREENCHIDOS **/
if (empty($contato) || empty($cdata)){
   echo "Os campos Contato, Data, NÃO podem conter valor nulo!";
   exit;
} 
/** ALTERA AS INFORMAÇÕES NA TABELA CONTRATO DO BANCO DE DADOS COMERCIAL **/
$PDO = conecta_bd();
$sql = "UPDATE contato SET cdata=:cdata,contato=:contato,obs=:obs WHERE idcont=:idcont AND cdata=:cdata";
$stmt = $PDO->prepare($sql);
$stmt->bindParam(':contato', $contato);
$stmt->bindParam(':obs', $obs);
$stmt->bindParam(':idcont', $idcont, PDO::PARAM_INT);
$stmt->bindParam(':cdata', $cdata);

if ($stmt->execute()){
   header('Location: contatos.php');
}
else{
   echo "Ocorreu um erro na alteração do contato!";
   print_r($stmt->errorInfo());
}

Does anyone know what I'm doing wrong for the change not to be performed?

    
asked by anonymous 11.06.2018 / 15:18

1 answer

0

The problem seems to be that placeholder cdata serves two purposes (researching and being the new value). The primary key should be enough to find the record in which case you should either remove cdata from your where or create two one placeholders for each purpose.

    
11.06.2018 / 15:40