PDA

View Full Version : record deletion


chinni
09-07-2002, 07:09 PM
hello,

I'm trying to delete the records in a table through a form.First, i'm displaying all the records and then at the end of each record set,there is a checkbox and when i check it, the record is set for deleted(Not actually deleted from the table)Now,i'm trying to display two buttons, cancel and delete.when i hit delete the record is permanently deleted and when i hit cancel, the record which is set for delete is cancelled .
I've been trying this since a week and i could'nt really succeed.Could somebody pls help me.

here is my code


<?php
//Database connection details
if ($HTTP_POST_VARS){

/*-- the form was submitted, lets process it --*/

/*-- get a count of how many ids we have to process --*/
$cnt = count($HTTP_POST_VARS['id']);

$delete_sql = "DELETE from TBL_RBSNEW where id=$RBS_ID";
$result = ibase_query($delete_sql);

/*-- cycle through the arrays and update the database table --*/

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;

$sql = "update TBL_RBSNEW SET ".
"RBS_RFPI = '{$HTTP_POST_VARS['rfpi'][$cntr]}', ".
"RBS_IPADDRESS = '{$HTTP_POST_VARS['ip_address'][$cntr]}', ".
"RBS_ENROLMENT = $enrolment, ".
"RBS_CIPHERING = $ciphering, ".
"RBS_MACADDRESS = '{$HTTP_POST_VARS['mac_address'][$cntr]}', ".
"RBS_SUBNETMASK = '{$HTTP_POST_VARS['subnet_mask'][$cntr]}', ".
"RBS_ROUTERIP = '{$HTTP_POST_VARS['router_ip'][$cntr]}', ".
"RBS_TFTPSERVERIPADDRESS = '{$HTTP_POST_VARS['tftp_server'][$cntr]}', ".
"RBS_TFTPFILENAME = '{$HTTP_POST_VARS['tftp_file'][$cntr]}' ".
"where RBS_ID = {$HTTP_POST_VARS['id'][$cntr]}";
if (count($delete))
$sql .= "RBS_DELETE = 'Y'";
$sql .= "where RBS_ID = {$HTTP_POST_VARS['id'][$cntr]}";
"where RBS_ID = {$HTTP_POST_VARS['id'][$cntr]}";

$results = ibase_query($sql);
if (!$results) {
/* the query failed*/
echo "Error executing the update statement.
Interbase Reported:".ibase_errmsg()."
".
"SQL=$sql
\n";
}
}
}

/*-- this section is always shown --*/

/*-- get all records from the table --*/
$select_sql = 'Select RBS_ID, RBS_TFTPSERVERIPADDRESS, '.
'RBS_TFTPFILENAME, RBS_ENROLMENT, RBS_CIPHERING, '.
'RBS_RFPI, RBS_IPADDRESS, RBS_MACADDRESS, RBS_SUBNETMASK, '.
'RBS_ROUTERIP from TBL_RBSNEW';
$result_id = ibase_query($select_sql) or die('Error retrieving records from TBL_RBS.
Interbase Reported: '.ibase_errmsg());
?>
Edit 'N' Delete RBS</p>
<form name="editRBS" action="<?=$HTTP_SERVER_VARS['PHP_SELF']?>" method="POST">
<?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;
$macaddress=$row->RBS_MACADDRESS;
$subnetmask=$row->RBS_SUBNETMASK;
$routerip=$row->RBS_ROUTERIP;
$enrolment=$row->RBS_ENROLMENT;
$tftpserver=$row->RBS_TFTPSERVERIPADDRESS;
$tftpfile=$row->RBS_TFTPFILENAME;
/*-- output this row in the table --*/
?>
<tr style="margin:0; padding:0;" align="left" valign="top">
<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="155" height="23">
<input type="text" name="mac_address[<?=$counter?>]" value="<?=$macaddress?>">
</td>
<td width="156" height="23">
<input type="text" name="subnet_mask[<?=$counter?>]" value="<?=$subnetmask?>">
</td>
<td width="156" height="23" align="left" valign="top">
<input type="text" name="router_ip[<?=$counter?>]" value="<?=$routerip?>">
</td>
<td width="156" height="23" align="left" valign="top">
<input type="text" name="tftp_server[<?=$counter?>]" value="<?=$tftpserver?>">
</td>
<td width="156" height="23" align="left" valign="top">
<input type="text" name="tftp_file[<?=$counter?>]" value="<?=$tftpfile?>">
</td>
<td width="69" height="23">
<?
if ($row->RBS_DELETE) {
echo"<input type=\"checkbox\" name=\"ok[$counter]\" value=\"1\">";
echo"<input type=\"checkbox\" name=\"cancel[$counter]\" value=\"1\">";
}
else
echo"<input type=\"checkbox\" name=\"delete\" value=\"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>
</body>
</html>

Hawk
09-08-2002, 10:35 AM
First, you have, in simplified pseudo-code so I can point out what's wrong:


// Set the query string.
$string = "update <tablename> SET blah = blah, foo = bar, yada = yada where id = $id";
// Add the delete flag.
if ( at least one 'delete' box is checked ) {
$string .= "RBS_DELETE = 'Y'";
}
// This line is always executed.
$sql .= "where id = $id";
// This line does absolutely nothing.
"where id = $id";



I'm not familiar with ibase, but "update ... set ... where ... where" may not be valid syntax. Even if it is, the SET may not carry over past the first WHERE.
You have an additional line of code after the last one which does nothing.
Your "if" statement is checking count(delete), which is pretty useless. It isn't tied to $cntr at all, and will make checking any delete box always satisfy the condition.
Even if you did tie it to $cntr, your count would be off, because not all entries are guaranteed to have an associated 'delete' variable in the form. It should use $counter like 'ok' and 'cancel' do.
'ok' and 'cancel' aren't handled anywhere, but this is presumably because you can't get 'delete' working yet.
Nitpicks:
$cnt, $cntr, $counter, count()... your variable names are easily confused and not very descriptive. You'd be better off using $i, $j, etc, as they are less similar, though equally useless descriptively. $currentRow, $totalRowCount, etc, would obviously be the best option.
You aren't escaping the stuff you're passing to your database. I can pass'; DELETE from TBL_RBSNEW; as my "router_ip" and your table bites the dust.

chinni
09-08-2002, 11:03 AM
Hello hawk,

Thanks for pointing all my mistakes.
well, if you dont mind, could you pls insert the code for cancel and delete buttons.

I would be really geatful to you.

I'm really confused.

Hope you would help me.

Thanks in advance.