Mysql problems with DELETE

-2

I am failing mysql by recognizing the delete function

 $code = '1234';

 $conn = new PDO('mysql:host=localhost;dbname=data', 'root');
 $stmt = $conn->prepare('SELECT * FROM codes WHERE code='.$code.'');
 $stmt->execute();

 $result = $stmt->fetchAll();

   foreach($result as $row) {

 extract($id);

 $del = "DELETE FROM codes WHERE id = '".$id."'";

 //$id é o id da coluna do BD
   }
 }
    
asked by anonymous 14.07.2017 / 21:46

2 answers

1

You are putting the string with the code to be executed but did not execute it, add the lines below, below the variable $ of

if ($conn->query($del) === TRUE) {
    echo "Apagado com sucesso";
} else {
    echo "Error ao apagar: " . $conn->error;
}
    
14.07.2017 / 21:52
0

If you need to go through a query to find the id you want to exclude, do so:

$rs = $con->query(“SELEC id, nome, email FROM pessoa”);
while($row = $rs->fetch(PDO::FETCH_OBJ)){
  if ($row->email == '[email protected]' ) {
      $id = $row->id;
  }
}

To perform a DELETE with the PDO:

$stmt = $con->prepare("DELETE FROM pessoa WHERE idpessoa = ?");
$stmt->bindParam(1, $id);
$stmt->execute();
    
14.07.2017 / 22:36