Log in

View Full Version : problem deleting particular row record



vineet
11-20-2008, 08:42 AM
hi all

i am working in admin section to manage records. There i have records from serial no.38 to 43. I have a option to delete each single record with delete button provided with each single record. The problem i m getting in deleting is

1) when i click delete buton at record no.38 then the record no.43 gets deleted.

2) Then again when i click delete buton at record no.39 then the record no.41 gets deleted.


this the code


<?php
if(isset($_REQUEST['del_item_x']))
{
$qry_del="select * from order_detail_table";
$result_del=mysql_query($qry_del);
//$row_del=mysql_fetch_array($result_del);
while($row_del=mysql_fetch_array($result_del))
{
$serial=$row_del['serial'];
$order_id=$row_del['order_id'];
$product_name=$row_del['product_name'];
//echo $serial;
//echo "test". $row_del['order_id'];
}
$qry_del="DELETE from order_detail_table where order_id=$order_id AND serial='$serial'";
echo $qry_del;

if(mysql_query($qry_del))
{
$msg="item deleted success";
}
else
{
$msg="item not deleted";
}
}

?>




vineet

tuga
11-20-2008, 08:54 AM
Well i'm not what you can call an expert but what i see...

You are getting the serial number with the while statement


$qry_del="select * from order_detail_table";
$result_del=mysql_query($qry_del);
while($row_del=mysql_fetch_array($result_del))
{
$serial=$row_del['serial'];
$order_id=$row_del['order_id'];
$product_name=$row_del['product_name'];
}

so of course you are gonna get as $serial the last record, so when you are deleting you will be deleting the last.

You should pass the serial or id from the form and then just do the delete.

$qry_del="DELETE from order_detail_table where order_id=$order_id AND serial='$serial'";
if(mysql_query($qry_del))
{
$msg="item deleted success";
}
else
{
$msg="item not deleted";
}

I hope it helps, i'm just a newbie

vineet
11-20-2008, 09:07 AM
hi tuga

the thing is if you solve my problem you are an expert for me. i m also a newbie.

can u be more clear with this line


You should pass the serial or id from the form and then just do the delete.


vineet

tuga
11-20-2008, 09:21 AM
I guess that you have a form showing some kind of table with records.

Then i guess you have some checkbox to select which record(s) to delete, usually i set the value of each box with the id and the name sequencial like name="checkbox1" and so on. Another thing i do is add a hidden field with the record numbers, so know how many records are on the form. So when you do a submit you pass the values you want to delete in the selected checboxes and the total number of records.

If you show the code for the form maybe i can help you better.

vineet
11-20-2008, 09:36 AM
hi tuga

actually the rows data is not entered through the form. its getting data inserted from other tables in the database through queries codes. Its a lengthy code otherwise i should have shown you.

vineet

tuga
11-20-2008, 09:47 AM
hi tuga

actually the rows data is not entered through the form. its getting data inserted from other tables in the database through queries codes. Its a lengthy code otherwise i should have shown you.

vineet

Well i cannot help you much like that.
When you press the delete button you should somehow pass the id you want to delete,if you do not pass it, how do you know which record to delete?

This will always fetch the last record if more than one, and not the one you want, unless this retrieves only one record.

$qry_del="select * from order_detail_table";
$result_del=mysql_query($qry_del);
while($row_del=mysql_fetch_array($result_del))
{
$serial=$row_del['serial'];
$order_id=$row_del['order_id'];
$product_name=$row_del['product_name'];
}


Sorry

vineet
11-20-2008, 09:57 AM
hi tuga

i understand you. you are right. i need to get the serial or id to pass.

when the rows get inserted into the table the serial number gets inserted automatically as its auto increment in database.

see when i click delete button and echo the qry then i get



delete from order_detail_table where order_id=67 and serial=40


here is where i m getting the trouble. i m getting the corect order id but i m not gettng the right serial no.

vineet

tuga
11-20-2008, 09:57 AM
try this


$qry_del="select * from order_detail_table";
$result_del=mysql_query($qry_del);
while($row_del=mysql_fetch_assoc($result_del))
{
$serial=$row_del['serial'];
$order_id=$row_del['order_id'];
$product_name=$row_del['product_name'];
}

the change
while($row_del=mysql_fetch_assoc($result_del))