Jump to content

deleting record not grabbing proper ID


mcerveni

Recommended Posts

When i delete a record in my table, it will delete always the top record first...

 

for example:

my category table results:

 

watch [delete]

mantel clock [delete]

wall clock [delete]

 

the delete button is a <input type="button" name="btnDeleteCat" value="Delete" onclick="deleteCategory();">

 

the <input type="hidden" id="rowCatId" value="<?php echo $row['cat_id'];?>">  grabs my id for each record.

 

so when i press delete on Mantel Clock, it will delete Watch,

if i press delete on wall clock, it will delete the watch instead.

it's always deleting the first record...

but when i display that hidden input id, it's retrieving the proper category id.

 

if i hit delete on the watch, it will delete watch.

if i hit delete on mantel clock, it will delete mantel clock...

 

so i'm not too sure why it's doing that...

 

here's my ajax and php code:

 

function deleteCategory() {

document.getElementById('action_message').innerHTML = '<img src="../images/ajaxLoaderBlack.gif" height="25px">  Deleting Cateogry...';

var catID = document.getElementById('rowCatId').value;

nocache = Math.random();

http.open('get', 'delete.php?id='+catID+'&nocache = '+nocache);
http.onreadystatechange = deleteReply;
http.send(null);
}
function deleteReply() {
if(http.readyState == 4){ 
var response2 = http.responseText;

document.getElementById('insert_response').innerHTML = ' ' +response2+ '';
document.getElementById('action_message').innerHTML = 'The category has been deleted.';

}
} 

 

 

php code:

 


$catID = $_GET['id'];

$sql = "DELETE FROM catTable where cat_id = '$catID' LIMIT 1 ";
$r = mysql_query($sql); //Execute the query.


//RELOAD CATEGORY TABLE TO SHOW UPDATES RESULTS. 
//form tag cannot be here because it wont display the table.  
$sql = "SELECT * FROM tbl_category";
$r= mysql_query($sql); //Execute the query.
  //$row= mysql_fetch_array($r);


//UPDATE TABLE TO SHOW NEW RESULTS 
echo '<br><br>';

  showCatResults_Ajax();


 

-Thanks in advance,

 

Mike

 

Link to comment
Share on other sites

I believe your problem is with this line:

 

<input type="hidden" id="rowCatId" value="<?php echo $row['cat_id'];?>">  

 

If you look at the above "id" will not be unique if you have more that one of these.  There for document.getElementById('rowCatId').value will return the first "id" it finds, which is why you always end up deleting the first one.

 

Rather than using a hidden field, I'd put the id into the button and do something like this:

<input type="button" value="Delete" id="<?php echo $row['cat_id'];?>" onclick="deleteCategory(this.id);">

 

When you click this button, it will pass the id to the deleteCategory function.  So you need to edit your deleteCategory function like this:

function deleteCategory(sID) {
...
http.open('get', 'delete.php?id='+sID+'&nocache = '+nocache);
...
}

 

Hope this helps!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.