
how to delete row using checkbox and button in php?
localhost=wampserver database name=sourabh table name=sonu and my code is .....
<?php
$link=mysql_connect("localhost", "root", "");
mysql_select_db("sourabh", $link);
$rows=mysql_query("select * from sonu");
$row=mysql_fetch_array($rows);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>delete2</title>
</head>
<form action="delete2.php" method="post" name="delete" >
<table width="100%" border="1">
<tr><td>select</td>
<td>NAME</td>
<td>Father NAME</td>
<td>Date Of Birth</td>
<td>Mobile</td>
<td>Email</td>
<td>Address</td>
</tr>
<?php while($row=mysql_fetch_array($rows))
{ ?>
<tr>
<td><input type="checkbox" name="checkbox[]" id="checkbox[]" value="<?php echo $row['id'];?>"></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['fathername']; ?></td>
<td><?php echo $row['dob']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['address']; ?></td>
</tr>
<?php } ?>
</table>
<input type="submit" name="delete" value="delete" />
<?php
if(isset($_POST['delete']))
{
for($i=0;$i<$row;$i++)
{
$delete = $checkbox[$i];
mysql_query("delete from sonu where id='$delete'");
}
}
?>
</form>
<body>
</body>
</html>

We are providing you the jsp code that displays the database table data into the html table. Along each table row there is a checkbox consists of id of table. When the user selects the particular checkbox, that row will get deleted from the database. In the database we have created three fields bookid,author and title in the database.
1) Create book.jsp
<%@page import="java.sql.*"%>
<form name=myname method=post action="delete.jsp">
<table border="1">
<tr><td></td>
<td><b><u>bookid</u></b></td>
<td><b><u>Author</u></b></td>
<td><b><u>title</u></b></td>
</tr>
<%try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "root");
ResultSet rs = null;
Statement st=null;
st=conn.createStatement();
rs = st.executeQuery("select * from book");
int i=0; while(rs.next()){ %>
<tr><td><input type="checkbox" name="check<%=i%>" value=<%= rs.getString("bookid") %>></td>
<td><%= rs.getString("bookid") %></td>
<td><%= rs.getString("author") %></td>
<td><%= rs.getString("title") %></td>
</tr><%
i++;
}
}catch(SQLException e){ System.out.println(e.getMessage()); } %>
</table>
<input type="submit">
</form>
2) Create delete.jsp
<%@page import="java.sql.*"%>
<%String id[]= new String[10];
for(int i=0;i<10;i++){
id[i]=request.getParameter("check"+i);
out.println(id[i]);
}
%>
<%try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "root");
ResultSet rs = null;
Statement st=null;
st=conn.createStatement();
for(int a=0;a<10;a++){
st.executeUpdate("delete from book where bookid='"+id[a]+"'");
}
}catch(SQLException e){
System.out.println(e.getMessage());
}
%>