-3

I want to delete a row from my data-table so I need to get the id value from database and then delete it.

$id = $_GET['id'];
$result = mysqli_query($con, "DELETE FROM registre where id=$id");

This code doesn't get the id value from database, I don't have a row for id in my data-table so I need to get from to db and then delete it.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    "doesn't work for me" isn't very descriptive. What doesn't work? – Jay Blanchard Apr 22 '19 at 10:52
  • you have a syntax error it should be `$result=mysqli_query($con,"DELETE FROM registre where id=".$id);` – sassy_rog Apr 22 '19 at 10:53
  • Also, when using variables that are exterior to the script, make use of prepared statements. Would advise checking [Preventing Mysql Injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Diogo Santo Apr 22 '19 at 10:56
  • Are you sure the table name is correct ( 'registre' ) or it may be 'register' – Ganesh Apr 22 '19 at 10:56
  • 1
    **Warning:** You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](http://php.net/manual/en/pdo.prepared-statements.php) or by [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). – Dharman Apr 22 '19 at 10:58
  • What "doesn't work" about it? Is there an error? What is the value of `$id`? What is the value of `$result`? Does `mysqli_error` show you an error? You have to tell us what the problem *is* before we can help solve it. – David Apr 22 '19 at 10:59
  • @JayBlanchard the table in db have ( 'id','name','url' ) i need to get that id value and delete the row from mt datatable , but my code doesnt get the id value from the db – Ayoub Abouhilal Apr 22 '19 at 11:05
  • 1
    @AyoubAbouhilal: *"but my code doesnt get the id value from the db"* - The code you have doesn't "get" anything, it "deletes". If you're trying to "get" data then the query keyword you want is called `SELECT`. It's still really not clear what the actual problem is here. – David Apr 22 '19 at 11:06
  • Are you thinking that the $_GET superglobal automatically gets information from your database? – imposterSyndrome Apr 22 '19 at 12:22

2 Answers2

0

Try this using prepated statement :

$stmt = $mysqli->prepare("DELETE FROM registre where id= ?");
$bind = $stmt->bind_param('i', $id);
$exec = $stmt->execute();
Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43
-1

Try this maybe it helps

$id = $_GET['id'];
 $result = mysqli_query($con, 'DELETE FROM registre where id=' . $id);
ahmed
  • 21
  • 5