Delete Query
How to Delete records from DataBase.

To Delete records from MySQL DataBase.

Syntax:-
DELETE FROM table_name WHERE column_name=value

Example:-
DELETE FROM tbl_users WHERE id=5

This mysql query delete user record where id is 5

Note:- where clause is necessary for delete particular records otherwise all records will be delete from database.

Example 1:-

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
die('Mysql not connect ');

$database=mysql_select_db("test", $con);

if (!$database)
die('DataBase not connect ');

mysql_query("delete from info where id=5");

mysql_close($con);
?>

Note:- This script will delete records where id=5

Example 2:- (HTML form based example)

<?php
if(isset($_POST['Submit']))
{
$userid=$_POST['userid'];
$conn=mysql_connect('localhost','root','');
if(!$conn)
die('Mysql Not Connected');
$database=mysql_select_db('test',$conn);
if(!$database)
die('DataBase Not Connected');
$query="delete from info where userid='$userid'";
$data=mysql_query($query);
if(!data)
die('Error in Query');
else
echo "Record Successfully Delete";
}
?>

<html>
<head>
<title>Delete Record</title>
</head>
<body>
<form name="frm" method="post" action="">
<table border="1" align="center">
<tr>
<td width="118">Enter User ID </td>
<td width="257"><input name="userid" type="text" id="userid" /></td>
<td width="28"><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>


Previous
Next