Delete multiple rows using checkboxs in PHP

Last month i am working, how to delete multiple rows from mysql using php and also active or inactive multiple selected rows. This is good one example to delete, active and inactive multiple rows in mysql using javascript and php.

Preview Heritages.php



// JavaScript Document
//Check All Check Box
function CheckAll()
{
var i;
for(i=0;i<document.frmmain.elements.length; i++)
{
var elm = document.frmmain.elements[i];
if(elm.type == "checkbox" && elm.name != "chkCheckAll" )
{
elm.checked =document.frmmain.chkCheckAll.checked;
}
}
}
function Check()
{
var i;
var flag;
flag=1;
for(i=0;i<document.frmmain.elements.length; i++)
{
var elm = document.frmmain.elements[i];
if(elm.type == "checkbox" && elm.name != "chkCheckAll" )
{
if(elm.checked==false)
{
flag=0;
}
}
}
if(flag==1)
{
document.frmmain.chkCheckAll.checked=true;
}
else
{
document.frmmain.chkCheckAll.checked=false;
}
}
function checkBest()
{
if( (parseInt(document.frmmain.BestCount.value) + parseInt(numChecked())) > 3 )
{
alert("Only three properties can be selected as best proposal at a time. Please check your selection");
return false;
}
else
{
frmmain.submit();
}
}
//Confirm message to make Delete
function checkDelete()
{
rtn=confirm("Are you sure you want to Delete this record(s) ?");
if(rtn==false)
{
return false;
}
else
{
document.frmmain.actionVal.value="Delete";
//return true;
document.frmmain.submit();
}
}
//Confirm message to make Active
function checkApproval()
{
rtn=confirm("Are you sure you want to Active this record(s) ?");
if(rtn==false)
{
return false;
}
else
{
document.frmmain.actionVal.value="Active";
//return true;
document.frmmain.submit();
}
}
//Confirm message to make InActive
function checkReject()
{
rtn=confirm("Are you sure you want to Inactive this record(s) ?");
if(rtn==false)
{
return false;
}
else
{
document.frmmain.actionVal.value="Inactive";
//return true;
document.frmmain.submit();
}
}
function numChecked()
{
j=0;
for(i=0;i<document.frmmain.length;i++)
{
e=document.frmmain.elements[i];
if (e.type=='checkbox' && e.name != 'chkCheckAll' && e.checked)
j++;
}
return j;
}
function slct1st()
{
j=0;
for(i=0;i< document.frmmain.length;i++)
{
e=document.frmmain.elements[i];
if (e.type=='checkbox' && e.name != 'chkCheckAll' && e.checked)
if(j==1)
e.checked=false;
else
j=1;
}
return j;
}
//Submit function according click
function Subm(first,dosub,opt)
{
for(i=0;i<document.frmmain.length;i++)
{
e=document.frmmain.elements[i];
if (e.name == 'chkCheckAll')
e.checked=false;
}
num = ((first) ? slct1st() : numChecked());
if (num>0)
{
if(opt==1)
return checkDelete();
else if(opt==2)
return checkReject();
else if(opt==3)
return checkApproval();
else if(opt==4)
return checkBest();
}
else
{
alert("Please select at least one record.");
return false;
}
}
view raw checkbox.js hosted with ❤ by GitHub
<?php
//Database Connection
$con = mysql_connect('localhost', 'root', 'pinturp1');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sikandra_sikandrabaddb", $con);
include('updaterecords.php');
?>
<script type="text/javascript" src="js/checkbox.js"></script>
<form method="post" name="frmmain" id="frmmain" action="" class="nice">
<input type="hidden" name="actionVal" value="" />
<div style="width:150px; float:left;"><h2>Heritage List</h2></div>
<div style="width:330px; float:left;" align="right">
<a href="javascript: return false;" style="text-decoration:none">
<button class="styled" type="button" >Add Heritage</button>
</a>
<button class="styled" type="button" onclick="return Subm(0,0,1);">Delete </button>
<button class="styled" type="button" onclick="return Subm(0,0,3);">Active </button>
<button class="styled" type="button" onclick="return Subm(0,0,2);">Inactive </button>
</div>
<div style="clear:both;">
<table width="50%" border="0" cellpadding="0" cellspacing="0" id="data">
<?php
$qGetHeritages = mysql_query("select * from sikandrabad_heritages where Deleted=0 limit 0,15");
$iHeritage = mysql_num_rows($qGetHeritages);
?>
<tr>
<th width="5%" align="center" scope="col" class="nosort"><div align="center">
<input name="chkCheckAll" type="checkbox" value="checkbox" onclick="CheckAll();" />
</div></th>
<th width="25%" scope="col" align="left">Heritage Name</th>
<th width="16%" scope="col" align="left">Status</th>
</tr>
<?php
if($iHeritage==0)
{?>
<tr>
<td class="odd" width="100%" colspan="4" align="center">No Records Found</td>
</tr>
<?php } else {
$k="check";
while($aHeritage = mysql_fetch_array($qGetHeritages))
{
?>
<tr>
<td class="odd" width="5%" align="center"><input type="checkbox" name="<?php echo $k."_".$aHeritage['HeritageID']; ?>" id="<?php echo $k."_".$aHeritage['HeritageID']; ?>" /></td>
<td class="odd" width="55%"><?php echo $aHeritage['HeritageName'];?></td>
<td class="odd" width="20%"><?php if($aHeritage['Active'] == 0) echo "InActive"; else echo "Active";?></td>
</tr>
<?php } }?>
</table>
</form>
view raw heritages.php hosted with ❤ by GitHub
<?php
if($_POST)
{
/***************Starts : Multiple Active Reocrds************************/
if($_REQUEST['actionVal'] == "Active")
{
$QueryMaxId = mysql_query("select max(HeritageID) as HeritageID from sikandrabad_heritages");
$aHeritage = mysql_fetch_array($QueryMaxId);
$MaxId = $aHeritage['HeritageID'];
// Check if Rejected button active, start this
$totalid="";
for($k=0;$k<=$MaxId;$k++)
{
$c="check_".$k;
if($_POST[$c])
{
if($totalid=="")
{
$totalid=$k;
}
else
{
$totalid=$totalid.",".$k;
}
}
}
if($totalid != "")
{
//Active Selected Record In Table
$QueryUpdate="update sikandrabad_heritages set Active= 1 where HeritageID in ($totalid)";
if (!mysql_query($QueryUpdate,$con))
{
die('Error: ' . mysql_error());
}
//Redirect
header("location: heritages.php");
}
}
/***************Ends : Multiple Active Reocrds************************/
/***************Starts : Multiple Inactive Reocrds************************/
if($_REQUEST['actionVal'] == "Inactive")
{
//Fetch Id In Table
$QueryMaxId = mysql_query("select max(HeritageID) as HeritageID from sikandrabad_heritages");
$aHeritage = mysql_fetch_array($QueryMaxId);
$MaxId = $aHeritage['HeritageID'];
// Check if Rejected button active, start this
$totalid="";
for($k=0;$k<=$MaxId;$k++)
{
$c="check_".$k;
if($_POST[$c])
{
if($totalid=="")
{
$totalid=$k;
}
else
{
$totalid=$totalid.",".$k;
}
}
}
if($totalid != "")
{
//InActive Selected Record In Table
$QueryUpdate="update sikandrabad_heritages set Active= 0 where HeritageID in ($totalid)";
if (!mysql_query($QueryUpdate,$con))
{
die('Error: ' . mysql_error());
}
//Page Redirects
header("location: heritages.php");
}
}
/***************Ends : Multiple Inactive Reocrds************************/
/***************Starts : Multiple Delete Reocrds************************/
if($_REQUEST['actionVal'] == "Delete")
{
//Fetch Id In Table
$QueryMaxId = mysql_query("select max(HeritageID) as HeritageID from sikandrabad_heritages");
$aHeritage = mysql_fetch_array($QueryMaxId);
$MaxId = $aHeritage['HeritageID'];
// Check if delete button active, start this
$totalid="";
$t=0;
$aDeletedFiles = array();
for($k=0;$k<=$MaxId;$k++)
{
$c="check_".$k;
if($_POST[$c])
{
if($totalid=="")
{
$totalid=$k;
$aDeletedFiles[$t] = $k;
$t = $t +1;
}
else
{
$totalid=$totalid.",".$k;
$aDeletedFiles[$t] = $k;
$t = $t +1;
}
}
}
if($totalid != "")
{
//Delete Record from table
$QueryUpdate="update sikandrabad_heritages set Deleted = 1 where HeritageID in ($totalid)";
if (!mysql_query($QueryUpdate,$con))
{
die('Error: ' . mysql_error());
}
//Page Redirect
header("location: heritages.php");
}
}
/***************Ends : Multiple Delete Reocrds************************/
}
?>


If you like this or this is useful for you. Please comment us. Thank You

Comments

Popular posts from this blog

Login with facebook using coldfusion

Create CSV file in Coldfusion Using CFFile

Get Previous One Day Data in Sql Server