Home Sql Mysql-table MySQL PHP Query delete
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

MySQL PHP Query delete

Advertisement
Mysql PHP Query delete is used to execute Mysql function ( ) delete query from a database table.

MySQL PHP Query delete

     

Mysql PHP Query delete is used to execute Mysql function ( ) delete query from a database table. MySQL PHP Query is used to delete the records from the table 'MyTable' based on condition specified in Where Clause

Understand with Example

The Tutorial grasp you an illustrative example on 'MySQL PHP Query delete'. To understand the example we create a table 'MyTable' that has the required fieldnames and datatypes respectively.

Query for creating table named MyTable :

mysql> CREATE TABLE MyTable (
    ->              Empid int(10),
    ->              Empname varchar(60)
    ->              Salary int(90)
    ->            );
Query OK, 0 rows affected (0.13 sec)

Query for inserting data in table:

To add the records or rows to the table 'MyTable'  we use insert into query.

mysql>   insert into MyTable values(01,'Girish','20000');
Query OK, 1 row affected (0.02 sec)
mysql>     insert into MyTable values(02,'A','21000');
Query OK, 1 row affected (0.01 sec)
mysql>     insert into MyTable values(03,'C','22000');
Query OK, 1 row affected (0.00 sec)
mysql>     insert into MyTable values(04,'V','23000');
Query OK, 1 row affected (0.00 sec)
mysql>     insert into MyTable values(05,'B','24000');
Query OK, 1 row affected (0.00 sec)
mysql>     insert into MyTable values(06,'E','25000');
Query OK, 1 row affected (0.00 sec)
mysql>     insert into MyTable values(07,'Q','26000');
Query OK, 1 row affected (0.01 sec)
mysql>     insert into MyTable values(08,'W','27000');
Query OK, 1 row affected (0.01 sec)
mysql>     insert into MyTable values(09,'AS','28000');
Query OK, 1 row affected (0.00 sec)`

Query to view data inserted in table:

mysql> select * from mytable;
+-------+---------+--------+
| Empid | Empname | Salary |
+-------+---------+--------+
| 1     | Girish  | 20000  |
| 2     | A       | 21000  |
| 3     | C       | 22000  |
| 4     | V       | 23000  |
| 5     | B       | 24000  |
| 6     | E       | 25000  |
| 7     | Q       | 26000  |
| 8     | W       | 27000  |
| 9     | AS      | 28000  |
+-------+---------+--------+
9 rows in set (0.01 sec)

Deleting table data using php select:

The PHP server side scripts include a server side code that include the function deleteData ( ), that is used to delete a records from table using delete Syntax. The Where Clause specifies which record or records should be omitted. To get PHP to execute the statement we use mysql _query function ( ). The function send the Mysql delete function to table 'mytable' in the database and the output is given below the code.

<?php
echo "<table><tr>";
echo "<td>".getValue()."</td>";
deleteData(8);
echo "<td>".getValue()."</td>";
echo "</tr></table>";

function deleteData($id){
$database="girish";
$user="root";
$password="root";
$host="192.168.10.126";
$link= mysql_connect($host,$user,$password) or die("Could not connect: ".mysql_error());
mysql_select_db($database,$link) or die("Error in selecting the database:".mysql_error());
$sql="delete from mytable where empid= ".$id;
mysql_query($sql,$link) or exit("Sql Error".mysql_error());
}
function getValue(){
$database="girish";
$user="root";
$password="root";
$host="192.168.10.126";
$link= mysql_connect($host,$user,$password) or die("Could not connect: ".mysql_error());
mysql_select_db($database,$link) or die("Error in selecting the database:".mysql_error());
$sql="select * from mytable";
$sql_result=mysql_query($sql,$link) or exit("Sql Error".mysql_error());
$sql_num=mysql_num_rows($sql_result);
echo "<b>Table before Deleting data</b>";
echo "<table border=\"1\" bgcolor=\"#CCCCFF\" >";
echo "<tr>
<td><b>Empid</b></td>
<td><b>Empname</b></td>
<td><b>Salary</b></td>
</tr>" ;
while($sql_row=mysql_fetch_array($sql_result))
{
$Empid=$sql_row["Empid"];
$Empname=$sql_row["Empname"];
$Salary=$sql_row["Salary"];
echo "<tr>";
echo "<td>".$Empid."</td>";
echo "<td>".$Empname."</td>";
echo "<td>".$Salary."</td>";
echo "</tr>";
} 
echo "</table>";
}
?>


Output:

After the deletion of record from table 'mytable', the table is look like as

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 8, 2009

Ask Questions?    Discuss: MySQL PHP Query delete  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
prasad
December 6, 2011
ple's tell the delete row

<script type="application/javascript"> function getdetails(name,email,age,mobile,gender,buttonname) { document.getElementById("txtName").value=name; document.getElementById("txtEmail").value=email; document.getElementById("txtAge").value=age; document.getElementById("txtMobile").value=mobile; document.getElementById("submit").value=buttonname; } function removeRowFromTable() { var tbl = document.getElementById('tbl_Registration'); var lastRow = tbl.rows.length; if (lastRow > 2) tbl.deleteRow(lastRow - 1); } </script> <?php mysql_connect("localhost","root",""); mysql_select_db("test"); $btnValue = "Submit"; if(isset($_POST['submit'])) { $Submit = $_POST['submit']; //here we are checking Button value if button value Submit then Values insert into Database if($Submit=="Submit") { $Name = $_POST['txtName']; $Email = $_POST['txtEmail']; $Mobile = $_POST['txtMobile']; $Age = $_POST['txtAge']; $Gender = $_POST['rbGender']; /* *Here i am checking Mobile number in database *if $count =0 then record will insert in database other wise it will dispaly the error message ... */ $query = "Select id from tbl_Registration where mobile='$Mobile'"; $res = mysql_query($query); $count = mysql_num_rows($res); //Inserting if($count==0) { if($Name=="") { echo "Please Enter Valide Name"; } else { $qry = "insert into tbl_Registration(name,email,mobile,age,gender) Values('$Name','$Email','$Mobile','$Age','$Gender')"; $res1 = mysql_query($qry); if($res1) { echo "Inserted Successfully"; } else { echo "Not Inserted"; } } } else { echo "Mobile Number already existed"; } } //Update Coding else { $Id = $_GET[id]; $Name =$_POST['txtName']; $Email =$_POST['txtEmail']; $Mobile =$_POST['txtMobile']; $Age =$_POST['txtAge']; $Gender =$_POST['rbGender']; /*$query= "Selete id from tbl_Registration where mobile='$Mobile'"; $res= mysql_query(query); $count = mysql_num_rows($res); if($count==0 || $count==1) {*/ if(Name=="") { echo"Please Enter Valid Name"; } else { $qry="UPDATE tbl_Registration set name ='$Name',email='$Email',age='$Age',mobile='$Mobile',gender='$Gender' where id='$Id';"; $res= mysql_query($qry); if($res) { echo"Updated successfully"; } else { echo"Not updated"; } } /*} else { echo "Mobile Number Already esited..."; }*/ } } //Database values Retriving Start... //Select Query $selectQry = "select * from tbl_registration"; $selectRes = mysql_query($selectQry); $selectRow = mysql_fetch_array($selectRes); echo "<table border='1'> <tr> <th>id</th> <th >Name</th> <th >Email</th> <th >Mobile</th> <th >Age</th> <th >Gender</th> <th >Modify</th> <th >Delete</th> </tr>"; $count=0; while($selectRow = mysql_fetch_array($selectRes)) { $count=$count+1; echo "<tr> <td>$count </td> <td >$selectRow[1]</td> <td >$selectRow[2] </td> <td >$selectRow[3]</td> <td >$selectRow[4]</td> <td >$selectRow[5] </td> <td><a href=\"javascript:getdetails('$selectRow[1]','$selectRow[2]','$selectRow[3]','$selectRow[4]','$selectRow[5]','Update')\">Edit</a></td> <td><a href=\"javascript:deleteRow('$selectRow[1]','$selectRow[2]','$selectRow[3]','$selectRow[4]','$selectRow[5]',)\">Delete</a></td> </tr> "; } echo "</table><br><br>"; //Database values Retrived end... //Code Start //Here i am getting the ID from url.. //And dispaling the values based on ID into Controls $uid=$_GET[id]; if(isset($_GET[id])) { $btnValue="Update"; $id = $_GET['id']; $query1 = "Select * from tbl_Registration where id=$id"; $res1 = mysql_query($query1); $values = mysql_fetch_array($res1); } //Code End ?> <html> <body> <form name="frmRegistration" method="post" action="" onSubmit="return Validation()"> <table id="tblReg"> <tr> <td>Name *</td> <td> <input type="text" name="txtName" id="txtName" value=""/> </td> </tr> <tr> <td>Email *</td> <td> <input type="text" name="txtEmail" id="txtEmail" value=""/> </td> </tr> <tr> <td>Phone *</td> <td> <input type="text" name="txtMobile" id="txtMobile" value=""/> </td> </tr> <tr> <td>Age</td> <td> <input type="text" name="txtAge" id="txtAge" value=""/> </td> </tr> <tr> <td>Gender</td> <td> <input type="radio" name="rbGender" id="rbGender" value="Male" <?php if($values[5]=="Male"){echo "checked";}?>/>Male <input type="radio" name="rbGender" id="rbGender" value="Female" <?php if($values[5]=="Female"){echo "checked";}?>/>Female <input type="button" value="Delete [D]" onClick="deleteChecked();" /> </td> </tr> <tr> <td> <input id="submit" name="submit" type="submit" value="<?php echo $btnValue;?>" /> </td> </tr> </table> </form> </tr> </table> </body> </html>
aaa
January 11, 2012
aaa

<script>alert('document.cookie');</script>
ROCKESH RONITH
February 15, 2012
php developers

very nice !!! but its not more helpful for beginners...
DMCA.com