0

I try to Delete a row using id of the row, but my script doesn't work. Is there any syntax error ?

<?php

$PARAM_hote='aaaaaaa'; 
$PARAM_port='3306';
$PARAM_nom_bd='bbbbbbbbb'; 
$PARAM_utilisateur='cccccccccc'; 
$PARAM_mot_passe='ddddddddddd';
// Create connexion to BDD

$connexion = new PDO('mysql:host='.$PARAM_hote.';port='.$PARAM_port.';dbname='.$PARAM_nom_bd, $PARAM_utilisateur, $PARAM_mot_passe);

try {

    $id = $_POST['id'];

    // Prepare SQL request to check if the user is in BDD
    $sqlInsert = 'DELETE from annonces where id=:id';
    $resultats = $connexion->prepare($sqlInsert);
    $resultats->execute(array(':id' => $id));

    // Check if request is success

    if ($resultats) { 
        echo "success";
    }
    // or if the request is failed
    else {
        echo "fail";
    }

} catch(Exception $e) {

    echo 'Erreur : '.$e->getMessage().'<br />';

    echo 'N° : '.$e->getCode();

}



?>
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166
  • By default, PDO errors silently (except the connection) Read [How to squeeze an error message out of PDO](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) and setup your `$connexion` to use `PDO::ERRMODE_EXCEPTION` so it throws useful exceptions (which you're trying to catch) – Michael Berkowski Jun 05 '14 at 19:39
  • Try checking the return status of your PDO calls for errors. That's a much more reliable way of debugging your code. –  Jun 05 '14 at 19:39
  • Also, as you have it now, `$resultats` will never be false when checking `if ($resultats)`. It will always be a PDO object, though it may be in an error state. Setting up exception handling will be ideal though. – Michael Berkowski Jun 05 '14 at 19:40

0 Answers0