Dear members how i can delete a record from a table in php?Give me the code plz...

Dani AI

Generated

A few important points before a copy/paste snippet: a DELETE is just an SQL statement, but in a web app you must guard against accidents and attackers. Always supply a WHERE clause (or you will remove every row), prefer deleting by a primary key, and use parameterized queries to avoid SQL injection. As noted, the action is SQL-driven; as showed, putting variables straight into the SQL is risky — use prepared statements instead and test on a development database first.

Example using PDO (safer and works on modern PHP):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=yourdb', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->prepare('DELETE FROM users WHERE id = :id');
$stmt->execute([':id' => (int) $id]);

if ($stmt->rowCount() > 0) {
    // deleted
} else {
    // nothing matched
}
?>

Practical tips: wrap multiple related deletes in a transaction so you can roll back on error; prefer foreign keys with ON DELETE CASCADE when child rows should follow parent deletes; confirm deletions in the UI and protect form-triggered deletes with CSRF tokens; log or archive data if it must be recoverable. Avoid the old mysql_* functions (removed in PHP 7); use PDO or mysqli prepared statements instead. For reference, see the PHP docs on prepared statements (PDO prepared statements) and the SQL DELETE syntax (MySQL DELETE).

Recommended Answers

All 2 Replies

there is nothing special or specific to PHP,
use your normal SQL statement with PHP for example with MySQL

mysql_query("DELETE FROM my_table WHERE id = 1");

try modify this code to suit what you want

$name= "record to delete"

 
$sql = "DELETE FROM Users WHERE Name= '$name'"or die ("ERROR: Cannot not find user!"); 


$result = mysql_query($sql) or die ("Error in query: $sql. ".mysql_error());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.