Forums

PHP

This topic is locked

delete multiple checkbox using php

Posted 23 Aug 2006 08:08:20
1
has voted
23 Aug 2006 08:08:20 ct aja posted:
here are my coding : i wanna to delete multiple checkbox using php.....

----delete_multiple.php-----


<html>
<head>
<?php
$name = $_POST["name"];
$password = $_POST["password"];

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="1234"; // Mysql password
$db_name="hardware"; // Database name
$tbl_name="user"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password"or die("cannot connect";
mysql_select_db("$db_name"or die("cannot select DB";

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="deleteRecord.php">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>user_id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>password</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>phone</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>position</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>department</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['name']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['user_id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['phone']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['position']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['department']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE name='$del_id'";
$result = mysql_query($sql);
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>


then..post to here:

---deleteRecord.php---


<html>
<head>
<?php
$name=$_GET["name"];

//connect to mysql server
$db=mysql_connect("localhost","root","1234"
or die
("could not connect to database, please check that mysql is running";

//$db=mysql_pconnect();
mysql_select_db("hardware";//database name
$query="DELETE FROM user WHERE name='".$name."'";
$ex=mysql_query($query);
//$num_rows=mysql_num_rows($result);
?>

<title>staff registrationelete Record</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>


<body>
<p>The record for <? echo $name ?> has been deleted </p>
<p><a href="dinamic_pag.php">View List</a> </p>
</body>
</html>

and last.....view as a pagination page:

----dinamic_pag.php-----

<?
//This script uses Ajax to browse a query's result, using dinamic pagination.
//you need this in order to use Ajax with minor efforts
require("Sajax.php";

//database connection
//configure it at your own needs
$server = "localhost";
$usr = "root";
$pwd = "1234";
$db = "hardware";

$conn = mysql_connect($server, $usr, $pwd)
or die("Error connecting to: $server";

$db = mysql_select_db($db, $conn)
or die("Error opening database: $db";

//this function does all the job
//TO USE IT, YOU ONLY NEED TO:
// - modify the 2 db's queries inside de function
//OPTIONALLY:
// - in the javascript section you can modify the
// list of rows_per_page you want to show to the user
//
function paginate($rows_per_page, $start) {

//navigation variables to restrict movement inside results
$nav_back = 0;
$nav_forward = 0;

//delay (used for debugging)
sleep(1);

//this variable holds the table's' code that shows the results
$s_out = "document.getElementById(\"table\".innerHTML='" .
"<table align=\"CENTER%\" width=\"100%\" cellpadding=\"0\" cellspacing=\"1\" border=\"1\">" .
"<thead>";

//query the number of records
$sql = "SELECT COUNT(*) FROM user";

$result = mysql_query($sql);

$rs = mysql_fetch_array($result);

$total_rows = $rs[0];

if (($total_rows > 0) && ($start < $total_rows)) {

//main query, don't forget to use 'order by' statement to get a good pagination
$sql = "SELECT * " .
"FROM user " .
"ORDER BY user_id " .
"LIMIT $start,$rows_per_page";

$result = mysql_query($sql);

$num_fields = mysql_num_fields($result);

//table's header
$s_out .= "<tr>";

for ($j=0;$j<$num_fields;$j++) {

$s_out .= "<th>" . mysql_field_name($result, $j) . "</th>";
}

$s_out .= "</tr>" .
"</thead>" .
"<tbody>";

while ($rs = mysql_fetch_array($result)) {

$salida .= "<tr>";

for ($j=0;$j<$num_fields;$j++) {

$s_out .= "<td>" . $rs[$j] . "</td>";
}

$s_out .= "</tr>";
}

//end of table
$s_out .= "</tbody>" .
"</table>';";

//can we go back in the results?
$nav_back = intval($start >= $rows_per_page);

//can we go forward in the results?
$nav_forward = intval($start + $rows_per_page < $total_rows);

//return the position and total records in the page
$s_out .= "document.getElementById(\"status\".innerHTML='Showing Results " .
($start + 1) . " - " . min($start + $rows_per_page, $total_rows) .
" of $total_rows';" .

//return browsing options
$s_out .= "nav_back=$nav_back;nav_forward=$nav_forward;";

return $s_out;
}
}

//starting SAJAX stuff
//$sajax_debug_mode = 1;
$sajax_request_type = "POST";
sajax_init();
sajax_export("paginate";
sajax_handle_client_request();
?>
<html>
<head>

<link type="text/css" rel="stylesheet" href="./nice_style.css">

<script>
<?
sajax_show_javascript();
?>

//put in this array the options you want to show in the rows_per_page's list
var a_options_rpp = new Array(5, 10, 20, 30, 50, 100, 200);

//initial rows you want to show in the page
var rows_per_page = a_options_rpp[0];

//use this if you want to control impatient users
var working = false;

//opciones de navegacion activas
var start = 0;
var nav_back = 0;
var nav_forward = 0;

function paginate_cb(s) {

eval(s);

working = false;
}

function paginate() {

document.getElementById("status".innerHTML = 'Query in Progress...';

working = true;

x_paginate(rows_per_page, start, paginate_cb);
}

function back() {

if (working)
return;

if (!nav_back) {

alert('You are at begin of results!');
return;
}

start -= parseInt(rows_per_page);

paginate();
}

function forward() {

if (working)
return;

if (!nav_forward) {

alert('No more results!');
return;
}

start += parseInt(rows_per_page);

paginate();
}

function adjustPaginate(rpp) {

rows_per_page = rpp;

start -= (start % rows_per_page);

paginate();
}

function fillListRPP(lst) {

for (i=0;i<a_options_rpp.length;i++) {
lst.options[i] = new Option(a_options_rpp[i],a_options_rpp[i]);
}
}

</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
<!--
body {
background-color: #FFFFCC;
}
-->
</style></head>
<body onLoad="fillListRPP(document.f.l);paginate();">
<p>WELCOME, <? echo $name ?></p>
<form name="f">

<input type="button" value="Back" onClick="back();">
<input type="button" value="Next" onClick="forward();">

<i>Rows per Page:</i>
<select name="l" onChange="adjustPaginate(this.value);">
</select>

</form>

<div id="status" style="{text-align:center;font-style:italic}"></div>

<div id="table"></div>

</body>
</html>


i need somebody to help me.....my delete button not function....plz.....

thanks....

Replies

Replied 23 Aug 2006 14:36:18
23 Aug 2006 14:36:18 Roddy Dairion replied:
wat u need is to retrieve all the id thats present in your checkbox. Then using an array() you can simply loop to delete the id selected.
Replied 24 Aug 2006 04:19:06
24 Aug 2006 04:19:06 ct aja replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
wat u need is to retrieve all the id thats present in your checkbox. Then using an array() you can simply loop to delete the id selected.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>


thanks for ur reply yeah.....emmm...could u please show me how to do it..i have no idea right now..

thanks again......

<img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>
Replied 24 Aug 2006 12:00:03
24 Aug 2006 12:00:03 Roddy Dairion replied:
This is you form
<pre id=code><font face=courier size=2 id=code>
&lt;form name="multipledelete" method="post"&gt;
&lt;?php
//your sql query
$select = "select * from tablename";
//execute you query
$query = mysql_query($select);

//creating repeat region
while ($row = mysql_fetch_array($query))
{
?&gt;
&lt;input type="checkbox" name=&lt;?='delete[]'?&gt; value="&lt;?=$row['id']?&gt;"&gt;
&lt;?php
}
?&gt;

&lt;/form&gt;
</font id=code></pre id=code>
this is the query that will execute it
<pre id=code><font face=courier size=2 id=code>
&lt;?php
//your connection properties

$checkboxes = $_POST['delete'];

//count how many id is actually present
$countchkboxes = count($checkboxes);

//use the for loop to retrieve each id
for ($i = 0; $i &lt; $countchkboxes; $i++)
{
//retrieve value for ids and leave a comma after each one.
//if value is blank then don't put a comma
$retrievecb.= (($retrievecb != '') ? ", " : "" . intval($countchkboxes[$i];
}

//delete query
$delete = mysql_query("delete from table where fieldname IN (".$retrievecb."";

//Done your query will be executed and all the id retrieve has been stored in the variable $retrievecb and will be deleted in the query.

?&gt;
</font id=code></pre id=code>
Replied 29 Aug 2006 07:14:37
29 Aug 2006 07:14:37 ct aja replied:
please help me!!!!!!

it is like this......

&lt;form name="multipledelete" method="post"&gt;
&lt;?php
//your sql query
$select = "select * from user";
//execute you query
$query = mysql_query($select);
//creating repeat region
while ($row = mysql_fetch_array($query))
{
?&gt;
&lt;input type="checkbox" name=&lt;?='delete[]'?&gt; value="&lt;?=$row['user_id']?&gt;"&gt;
&lt;?php
}
?&gt;
&lt;/form&gt;

&lt;?php
//your connection properties

$checkboxes = $_POST['delete'];
//count how many id is actually present
$countchkboxes = count($checkboxes);
//use the for loop to retrieve each id
for ($i = 0; $i &lt; $countchkboxes; $i++)
{
//retrieve value for ids and leave a comma after each one.
//if value is blank then don't put a comma
$retrievecb.= (($retrievecb != '') ? "user_id" : "" . intval($countchkboxes[$i];
}
//delete query
$delete = mysql_query("delete from user where user_id IN (".$retrievecb."";
//Done your query will be executed and all the id retrieve has been stored in the variable $retrievecb and will be deleted in the query.
?&gt;
Replied 29 Aug 2006 14:06:26
29 Aug 2006 14:06:26 Roddy Dairion replied:
no you've done something wrong in the code i see that you've done this
<pre id=code><font face=courier size=2 id=code> $retrievecb.= (($retrievecb != '') ? "user_id" : "" . intval($countchkboxes[$i];</font id=code></pre id=code>
this is wrong just take the code i've given to you and use it as it is, you can modify the var names. the line above basically will retrieve the id present in the array and put a comma after each id except the last but you've modified so that after each id it add the text user_id.
Replied 22 Sep 2006 07:13:31
22 Sep 2006 07:13:31 violet velvish replied:
roddy..i hv d same prob also..i'd copy 100% ur code inside mine..
<pre id=code><font face=courier size=2 id=code> $retrievecb.= (($retrievecb != '') ? ", " : "" . intval($countchkboxes[$i]; </font id=code></pre id=code>
but i got parse error..then i alter that code to
<pre id=code><font face=courier size=2 id=code>$retrievecb.= (($retrievecb != '') ? ", " : "" . intval($countchkboxes[$i]);</font id=code></pre id=code>
after that i got error- undefine variable retrievecb
hmm..<img src=../images/dmxzone/forum/icon_smile_dissapprove.gif border=0 align=middle>
Replied 02 Oct 2006 06:49:38
02 Oct 2006 06:49:38 mut main replied:
Here is mine works fine:

$checked = $_POST['list_id'];
foreach ($checked as $delete) {

if ((isset($_POST['list_id'])) && ($_POST['list_id'] != "") {
$deleteSQL = sprintf("DELETE FROM listing WHERE list_id=$delete",
GetSQLValueString($_POST['list_id'], "int");

mysql_select_db($database_yourdataconnection, $yourdataconection);
$Result1 = mysql_query($deleteSQL, $yourdataconection) or die(mysql_error());


$deleteGoTo = "listing.php?" . $row_Recordset1['id'] . "";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}

header(sprintf("Location: %s", $deleteGoTo));

}
}

This is my checkbox part

&lt;input type="checkbox" name="list_id[]" value="&lt;?php echo $row_Recordset2['list_id']; ?&gt;" /&gt;

You have to make the "name" an array

Reply to this topic