-1

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

swydell
  • 1,962
  • 8
  • 31
  • 44

1 Answers1

1

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

and remove the quotes round the ':name_id'

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
alzee
  • 1,393
  • 1
  • 10
  • 21
  • Also, parameterized queries don't need parameters quoted. – miken32 Feb 02 '17 at 22:00
  • So if I remove the * and the quotes, it should work? – swydell Feb 02 '17 at 22:01
  • Also change `name_id=':name_id'` to `name_id=:name_id` – RiggsFolly Feb 02 '17 at 22:07
  • Netbeans underlines the line red when I remove the single quotes in the parameter to show an error. I took out the *. My row is still doesn't delete. – swydell Feb 02 '17 at 22:11
  • I did as everbody suggested but no luck. Free-ii- has marked my question as a duplicate. I know it is similar. I kind of hinted at that in my question when I said I read many examples. My code seems to be exactly as described in many answers and examples, but it is not working for me. – swydell Feb 02 '17 at 22:22
  • The reason why I haven't given up is because all the examples I have followed are really simple and the code is small, no more than nine or line lines. There is one tutorial that I followed exactly and it worked for me. When I click the delete link the row deletes. That code however used functions and public and private access modifiers. I think I got the term correct. My code is much simpler so it should work. It's driving my crazy. – swydell Feb 02 '17 at 22:48
  • The TRUNCATE clause works. When I use it TRUNCATE TABEL student, all the row are removed from the table and the table rows initialize to 1. When I don't use the WHERE clause all the rows are deleted but the row count starts after the last row in the table.. – swydell Feb 02 '17 at 23:20