I can not deleting single data from database

is ther anything wrong .what can i do?code are follwing

<?php
include_once('show.php');
$id="";
$name="";
$fathername="";
$mothername="";
$address="";
$birthday="";
$age="";
$email="";
$religion="";
$nationality="";
$gender="";
	

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "registrationform";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// sql to delete a record

$sql="DELETE FROM registrationdb WHERE id=$id";

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

$conn->close();

?>

I assume you have some records in your database?

$sql="DELETE FROM registrationdb WHERE id=id";

What’s the value of id?

First, you have initiated variable $id as blank at the top, so from where you will be getting $id

Second, in DELETE query, you missed “$” for id variable

$sql="DELETE FROM registrationdb WHERE id='$id' ";

In fact, I’d be surprised if running the WHERE id = id query didn’t cause the same result as running a TRUNCATE registrationdb query.

i did this but still is’n working
$sql="DELETE FROM registrationdb WHERE id=‘$id’ "

its deleting all rows.but i want to delete a single row.how can i do it?

when i give the value of id it did work.but i want something else like delete a row.but it delete all row of my table

did i miss something on that.
If i want to delete a data how it works

You need to give a value to the $id variable.

You need to use a prepared statement for the query otherwise you leave yourself wide open to SQL Injection attacks

<?php
include_once('show.php');
$id="";
$name="";
$fathername="";
$mothername="";
$address="";
$birthday="";
$age="";
$email="";
$religion="";
$nationality="";
$gender="";
	

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "registrationform";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// sql to delete a record

$sql="DELETE FROM registrationdb WHERE id='$id'";

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

$conn->close();

?>

still isn’t working it.

As I posted previously

So if you ran that query, because id = id will be true for all rows, you have deleted all of the rows in your table.

You will need to restore your backup or INSERT some rows before you can test DELETE again.

1 Like

i did it but still cant delete a row although it giving no error and showing delete successfully.but did not delete a row actually.

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "registrationform";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// sql to delete a record
$id=$_GET["id"];
$sql="DELETE FROM registrationdb WHERE id='$id'";

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

$conn->close();

please see any problem with that.

Try echoing $sql just after you set it to see if it’s the value you expect.

just echo the $sql query and check whether the id is getting the value or not

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.