PDA

View Full Version : Deleting a record(s) from a table


chinni
09-03-2002, 10:45 AM
Hello everybody,

I display all the records from a table on a PHP form.Now, i would like to delete record(s) in a table using the same form.
Here it goes how i really want to implement this one:

1.First, i dont want to delete the record in a table in a single-step.
2.(when i displayed the records from a table, i also display a check box at the end of each record).

3.Now, first, i want to check this box and click a button.when i do this, i want to set a value to this row in the table(I've inserted a column called delte in the table)So, this column will have a value 0 or 1 when this row has been selected to delete.
4.After the value has been set, the form will show two buttons or images which has the values cancel delete or delete the record.

so, thatz it.I would be really appreciate if somebody can help me how can i get it this done!

First thing, i would really like to do is to set the value to the delete column when the check box has been checked.

Thanks in advance.

Uranium-235
09-03-2002, 12:57 PM
You need to generate the checkbox and identify it with a value="specific name to that row". Are you talking about a single deletion, or do you want to be able to check multiple boxes and click Delete changing the values of all those you selected? The name on the checkbox would have to have [] at the end so you can pass it back into the script as an array of the value's in each checkbox you clicked. if you're identifying them by id of each row you would go

<input type="checkbox" name="deletethis[]" value="2"> //2 is the ID of the row, probably generated in another loop

and you would hit a special button that would delete it. You could do somthing like this



<?php

for($i = 0; $i < count($deletethis); $i++)
{
$id=$deletethis[$i]
mysql_query("UPDATE row SET delte = '1' WHERE id='$id'");
}

?>

chinni
09-03-2002, 02:40 PM
hi there,

well, this is my simple code:I also inserted my question at the end, i.e at the place where i am confused(checkbox)


<?php
Database connection details

if ($HTTP_POST_VARS){
$cnt = count($HTTP_POST_VARS['id']);
for($cntr=0;$cntr<$cnt;$cntr++) {
if (!$HTTP_POST_VARS['enrolment'][$cntr]) $enrolment=0; else $enrolment=1;
if (!$HTTP_POST_VARS['ciphering'][$cntr]) $ciphering=0; else $ciphering=1;

$update_sql = "update TBL_RBS SET ".
"RBS_RFPI = '{$HTTP_POST_VARS['rfpi'][$cntr]}', ".
"RBS_IPADDRESS = '{$HTTP_POST_VARS['ip_address'][$cntr]}', ".
"RBS_ENROLMENT = $enrolment, ".
"RBS_CIPHERING = $ciphering, ".
"RBS_TFTPFILENAME = '{$HTTP_POST_VARS['tftp_file'][$cntr]}' ".
"where RBS_ID = {$HTTP_POST_VARS['id'][$cntr]}";

$update_results = ibase_query($update_sql);
}
}

$select_sql = 'Select RBS_ID, '.
'RBS_TFTPFILENAME, RBS_ENROLMENT, RBS_CIPHERING, '.
'RBS_RFPI, RBS_IPADDRESS from TBL_RBS';

$result_id = ibase_query($select_sql);

?>
<p class="rbsnormalbold">Edit RBS</p>
<hr style="line-height:100%; margin-top:0px; margin-bottom:0px;">
<form name="editRBS" action="<?=$HTTP_SERVER_VARS['PHP_SELF']?>" method="POST">
<table cellpadding="0" cellspacing="0" width="909" class="rbsnormal">
<tr style="margin:0; padding:0;" align="left" valign="top" class="rbsnormal">
<td width="154">
RFPI
</td>
<td width="154">
IP Address
</td>
<td width="69">
Enrolment
</td>
<td width="65">
Ciphering
</td>
<td width="96">
TFTP FILE
</td>
<td width="96">
Delete
</td>
</tr>
<?php
$counter=0;
$old_values=null;
while ($row = ibase_fetch_object($result_id)) {
$rfpi=$row->RBS_RFPI;
$ciphering=$row->RBS_CIPHERING;
$ipaddress=$row->RBS_IPADDRESS;
$enrolment=$row->RBS_ENROLMENT;
$tftpfile=$row->RBS_TFTPFILENAME;
?>
<tr style="margin:0; padding:0;" align="left" valign="top" class="rbsnormal">
<td width="156" height="23">
<input type="text" name="rfpi[<?=$counter?>]" value="<?=$rfpi?>">
</td>
<td width="154" height="23">
<input type="text" name="ip_address[<?=$counter?>]" value="<?=$ipaddress?>">
</td>
<td width="69" height="23">
<input type="checkbox" name="enrolment[<?=$counter?>]" value="1"<? if ($enrolment==1)echo "checked";?>>
</td>
<td width="65" height="23">
<input type="checkbox" name="ciphering[<?=$counter?>]" value="1" <? if ($ciphering==1)echo "checked";?>>
</td>
<td width="156" height="23" align="left" valign="top">
<input type="text" name="tftp_file[<?=$counter?>]" value="<?=$tftpfile?>">
</td>
<td width="65" height="23">
<input type="checkbox" name="delete_this" value="1">-->This is my check box and when i "check" this one and hit the submit button, the delete column corresponding to the selected row in the table is set to some value(0 or 1).
</td>

</tr>
<?php

$old_values.=' <input type="hidden" name="id['.$counter.']" value="'.$row->RBS_ID.'">
';
$counter++;
}
?>
</table>
<?=$old_values?>
<input type="submit" name="submit" value="Update">

<input type="hidden" value="Reset">
</form>
<p class="rbsnormal"> </p>
<p class="rbsnormal"> </p>
<p class="rbsnormal"> </p>
<p class="rbsnormal"> </p>
<p class="rbsnormal"> </p>
<p class="rbsnormal"> </p>
</body>




The column in my table is delete and later at the next step, i would like to perform the delete based on this column.

hope you would help me to fix this.

thank you

Halide
09-03-2002, 03:32 PM
Let me ask for clarification:

After selecting the items to be deleted, will you then show a second page with these selected items (as a confirmation) and show a cancel and continue button or link?

chinni
09-03-2002, 05:14 PM
hmm....well, after the check box has been "checked" and click the button submit, the flag value will be set and then i want the same page to be displayed with two buttons: cancel and delete.(It does'nt matter if the checkbox is checked or not checked when the page is shown 2nd time because, the records will be deleted only based on the flag value).Now, when the user hit the delete button, all the records with the flag value set will be deleted and when the cancel button is hit, no records will be deleted and the flag value is also set to null or what ever value other than the value which is previously set.

Hope somebody would help me.

Thanks in advance