Search code examples
phppdodelete-row

how come my delete statement in pdo php crud doen't work


I've have followed a number of tutorials, read many questions in a number of forums about the delete statement and how to use it in pdo php. I have binded parameters, not binded parameters, used prepared functions, defined my variables and indexes. When I click the delete link on the index page the program redirects to the delete page with no errors but it doesn't delete the row from the table. I have made my program small with only two columns to make it as simple as possible. And I have used the exact syntax with each tutorial and example I followed until I'm exhausted over three day period. If anybody can show me what I'm not doing right I'd appreciate it. I'm going to post code from the index page with the link and the delete.php page. In all due respect Fred-ii- I have read all the answers. Truncate doesn't apply to me because I don't want to delete a table, only a row. I don't want to delete all the rows so I use the WHERE clause. Unlike other questions that are similar I am not getting any errors. I rewrote my code to what has been suggested but my delete syntax is still not deleting the row in my index page. My WHERE clause points to name_id which is a Primary key in my database.

     <p><td><a href="delete.php?name_id=<?php echo $row['name_id'];   ?>">Delete</a></td></p>
   And here is the code from the delete page.

  require_once 'debase.php';

 // Get the  name to delete
 if(isset($_GET['name_id'])){
 $name_id = $_GET['name_id'];
 try{
  //$dns ="mysql:host=localhost;dbname=$db;charset=$charset";
 $conn = new PDO($dsn,$user,$pass);
 $stmt = $conn->prepare("DELETE * FROM student WHERE name_id=':name_id'");
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $stmt->bindValue(':name_id',$name_id, PDO::PARAM_STR);
 $stmt->execute();

 } catch (PDOException $e){
 $error_message = $e->getMessage();
$conn = null;
}
}

enter image description here


Solution

  • Your SQL syntax is incorrect.
    You just DELETE FROM ... and not DELETE * FROM...

    and remove the quotes round the ':name_id'