SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    Non-Member
    Join Date
    Nov 2010
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 ... ?

  2. #2
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean, you want to delete multiple records from your database at once?

  3. #3
    Non-Member
    Join Date
    Nov 2010
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes exactly, how is it?

  4. #4
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    Non-Member
    Join Date
    Nov 2010
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    First get the form to work without JavaScript than worry about making it happen without refreshing the page using JavaScript.
    The only code I hate more than my own is everyone else's.

  7. #7
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by binboy View Post
    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
    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.


    Code:
     
    <?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.

    2) create a "Delete selected items" button in your form.

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

    4) the server side script will receive the id's to delete in $_POST['delMe'] (assuming method="post")

    5) process the array in 4) to delete the selected id's from the database

    Code:
     
    <?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).

  8. #8
    I'm not a human
    Join Date
    Aug 2006
    Location
    India
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    Code:
     
    <?php 
     
    $rows2Del = $_POST['someID'];
     
    foreach($rows2Del as $id) { 
          $query = 'delete from myTable where fldID = "'.$id.'"'; 
          //then run the query
    } 
    ?>
    You can even simplify that code as
    Bcoz in general, i don't recommend running queries inside a loop...
    Code:
    <?php 
     
    $rows2Del = $_POST['someID'];
    $comma_separated = implode(",", $rows2Del);
     
    $query = 'delete from myTable where fldID in ('.$comma_separated.')'; 
    //then run the query
     
    ?>
    Regards,
    Vijay
    Follow me on twitter @vijaycbe
    World Holiday Calander || My Cricket Blog

  9. #9
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, like in most cases, there is more than 1 way of doing things

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

    I wonder how much processing time you would be saving

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

  10. #10
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  11. #11
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by donboe View Post
    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.


  12. #12
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    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

  13. #13
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

    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.

  14. #14
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    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?

    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

  15. #15
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  16. #16
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I rest my case. If you don't see it you don't see it. What else is there to say

  17. #17
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and the defence rests its case as well your honour

  18. #18
    I'm not a human
    Join Date
    Aug 2006
    Location
    India
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    I wonder how much processing time you would be saving

    In my experience it would be extremely tiny and not worth considering.
    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..

    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...

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



    I too just rest my case..
    Regards,
    Vijay
    Follow me on twitter @vijaycbe
    World Holiday Calander || My Cricket Blog


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •