How is delete multiple row to mysql whit jquery and php?

hello
how is delete multiple row to mysql whit jquery and php?
did is like insert whit $ajax() to mysql or … ?

Do you mean, you want to delete multiple records from your database at once?

Yes exactly, how is it?

That depends how your page is set up. Are you using a form with checkboxes? Which records need to be deleted? Please be a bit more specific.

Ok, i want use of checkboxes and delete rows that election (select). after delete show successfully deleted. ex: you successfully deleted 3 rows.
Without refresh page.
Thanks

First get the form to work without JavaScript than worry about making it happen without refreshing the page using JavaScript.

I would suggest something along these lines

  1. generate a “delete” checkbox in your html form for each row of data retrieved from the database.
 
<?php 
while($row=mysql_fetch_assoc($rs)) { 
       echo '<input type="checkbox" name="delMe[]" value="'.$row['someID'].'" />'; 
}
?>

where someID is the id of the row in the database.

  1. create a “Delete selected items” button in your form.

  2. when the button in 2) is clicked an array of id’s to delete, called delMe, will be sent to the server side script.

  3. the server side script will receive the id’s to delete in $_POST[‘delMe’] (assuming method=“post”)

  4. process the array in 4) to delete the selected id’s from the database

 
<?php 
 
$rows2Del = $_POST['someID'];
 
foreach($rows2Del as $id) { 
      $query = 'delete from myTable where fldID = "'.$id.'"'; 
      //then run the query
} 
?>

to do this without a page refresh, you do all the server side processing with an ajax function.

it’s only a handful lines of code so I don’t see the need to do it with jquery. you can do it all with just plain vanilla javascript (for the ajax call).

You can even simplify that code as
Bcoz in general, i don’t recommend running queries inside a loop…

<?php 
 
$rows2Del = $_POST['someID'];
$comma_separated = implode(",", $rows2Del);
 
$query = 'delete from myTable where fldID in ('.$comma_separated.')'; 
//then run the query
 
?>

yep, like in most cases, there is more than 1 way of doing things :slight_smile:

the way I see it, it’s all much the same.

I wonder how much processing time you would be saving :scratch:

In my experience it would be extremely tiny and not worth considering.

I still wondering why this need to be done without page refresh? But that aside!

Although this indeed can be done in different ways, as Kalon already said, I have to agree with kvijayhari here, that using IN instead of = would be more practical. Why loop if not needed?

Off Topic:

I think we’re straying off topic now, because whether the op uses a loop or not is irrelevant to the desired outcome - to delete multiple rows.

but to answer your question, unless there is a significant measurable and/or quantifiable difference in performance or use of system resources between using a loop and not using a loop then imho it simply boils down to personal preference on whether to use a loop or not.

The point is, it can indeed be done both ways, but using IN has many advantages over using a loop thats all. It has way more to do with stability than with performance

what are those “advantages” and can you quantify any benefits. I think any benefits will be far too small in size, for me at least, to consider.

Stability? :scratch:

in what way?, given that say for example you want to delete 20 rows the processing time it will take will be extremely tiny whichever method you use.

You only talk about processing time!

advantages are:

  • built in validation
  • no abandoned records
  • no transactions needed

I don’t see those as “advantages” at all because you can do all that with both methods.

but we’re now totally off topic because the op will have the choice to choose whichever option they prefer to use now that both have been highlighted in this thread.

I’m not going to tell anyone which method they must use.

I will continue to post the method I prefer to use here and in other forums and I will continue to advise others that it is totally their choice which method they prefer to use.

maybe the pros and cons of each method should now be discussed in a separate thread by anyone wishing to continue it.

I rest my case. If you don’t see it you don’t see it. What else is there to say

and the defence rests its case as well your honour :slight_smile:

Off Topic:

I agree with u that there are lots of ways to do a thing… And i just mentioned that i don’t recommend and i’m not saying thats the way to do it… :wink:

And Regarding your processing time, just consider that u r having a online service where your users do the above mentioned operation and that would make more queries to execute and in my code i just run only one query…

I think we should code to save the server resource too… My case would be justified when u think that our code is going reside and execute in a shared environment… :slight_smile:

Sitepoint is also about helping to code better and just not only abt helping with code…

I too just rest my case… :cool: