UPDATE problem and WHERE clause with two conditions

Asked

Viewed 83 times

1

After a lot of research on the internet without getting a solution, I turn to the "university" on duty! I have two tables: contract (primary key) and contacts. Being the second linked to the first. I list the records of the first and select change in a record. For each Contract 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 its respective records in the contact table. Only that to change the contact, I pass the Idcont (primary key of the contract table) plus the date information of the contact table. When you click change nothing happens. Neither error message nor change. I have already reviewed all the code, changed bindParam method to bindValue, etc.

Follow the code of the amendment form.

<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 routine change 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 so the change won’t be performed?

1 answer

0

The problem seems to be the fact of the placeholder cdata serve two purposes (search and be the new value). The primary key should be enough to find the record in which case you should, remove cdata your Where or create two placeholders one for each purpose.

  • Cdata and icont are the composite primary key of the contact table. I’m finding out about placeholders and testing. Anything put here doubt or solution. Grateful.

  • @Jamesfk then use two, one with the new value and the other for research.

  • rray, cdata need not be changed, it would be ideal. I have performed tests with it only in the filter and it did not work. the placeholder is just an example of what should be filled by the user, need to display the current values of the fields and allow changing. I’ve already removed the cdata from the update, it hasn’t solved either.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.