SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Update PHP

    Hi everyone, I had previously posted a thread pertaining to "Checkboxes with multiple updates". I had originally wanted to use checkboxes to dictate how and when textfields were updating my database.

    I had desperately been looking for a turorial on this, and could find none.

    You should know that I did not get this to work. In fact, I realized that checkboxes didn't matter at all with updating records!

    My first error, - say you have five records showing from a database and you want three of them to update. Assuming you've made a form to "post" the data (I did not use _get) - and you check three checkboxes - only those checkboxes that where checked are sent through "post". The crux is though that all of the five records post as well.

    If you use phpinfo() to look at your post data you'll see that the checkboxes aren't porportionate to how many fields are sent.

    Why is this important you ask? (Is he just raving for no reason?) It's all about looping baby.

    In order to update multiple records form one form, you need to have an array that loops into one sql statment. Checkout this form:

    Code:
    <form method="POST" name="formstudent" id="formstudent"> // this is the form named "formstudent"
              
              
              <table width="100%" border="0">
                
                
                <tr align="center" valign="middle"> 
                  <td colspan="3" bgcolor="#FFFFCC">The yellow section is for Who's in Charge (Student and Academic Affairs). The information displayed is what is already in the database.<br>
    			  If you wish to Delete a record, check the record you wish to delete, select the "delete record" radio button and press "Submit".<br>
    			  If you wish to Update a record, type in the new information you want, select the "update record" radio button and press "Submit".</td>
                </tr>
                <?php do { ?> // This is the a loop to repeat all of the records for this particular recordset.
                <tr bgcolor="#FFFFCC"> 
                  <td align="center" valign="middle">Use this Checkbox to delete a record.<br>
                    <input name="chkstudentdel[]" type="checkbox" id="chkstudentdel[]2" value="<?php echo $row_rsstudentaffairs['TodayID']; ?>"> // This checkbox allows you to delete entries
                  </td>
                  <td colspan="2" align="center" valign="middle"> 
                    <textarea name="txtstudent[]" cols="60" rows="3" id="txtstudent[]"><?php echo $row_rsstudentaffairs['TodayInfo']; ?></textarea> // this is the text you want to update
                    <input name="studentid[]" type="hidden" id="studentid[]" value="<?php echo $row_rsstudentaffairs['TodayID']; ?>"> </td>  /this is the primary key for the record yo uwish to update.
                </tr>
                <?php } while ($row_rsstudentaffairs = mysql_fetch_assoc($rsstudentaffairs)); ?>
                <tr align="center" valign="middle" bgcolor="#FFFFCC"> 
                  <td>Select the function you wish to execute:</td>
                  <td width="44%"><input type="radio" name="studentgoal1" value="1">
                    Delete Record 
                    <input name="studentgoal2" type="radio" value="2">
                    Update Record</td>
                  <td width="21%"><input type="submit" name="Submit" value="Submit"></td>
                </tr>
              </table>
              
            </form>
    I hope it's easy to notice what I'm doing.

    I've made form name "formstudent" that has a checkbox name "chkstudentdel[]" (notice the brackets). I used this to delete multiple entries. You can find this tutorial all over the web.

    The other two fields are "txtstudent[]" (notice the brackets) - this is the textbox that you type into, and the other is "studentid[]" (notice the brackets). In all of these cases I've used brackets to tell php that I want these values sent as an array. When this page posts, it sends an even number of textboxes and id's so that I can loop the variable through an array in my sql statement.

    Take a look at the php script.

    Ether you select a radio button "studentgoal1" to delete, or "studentgoal2" to update.
    Code:
    <?php
    if (isset($_POST['studentgoal1'])) //select the radio button delete, then submit
    {
          $ids_to_delete = join(',' , $_POST['chkstudentdel']); //sql for deleting follows join 
           mysql_query("DELETE FROM tblbcmttoday WHERE TodayID IN ($ids_to_delete)");
    }
    elseif (isset($_POST['studentgoal2'])) //select the radio button update, then submit
    {
    		for ($i = 0; $i < count($_POST['studentid']); $i++) // if it finds a id, then loop through and update
    {
    	  	mysql_query("UPDATE tblbcmttoday Set TodayInfo = '".$_POST['txtstudent'][$i]."' Where TodayID = '".$_POST['studentid'][$i]."'");
    }
    }
    ?>
    So, what's happening is all the records are being updated, but since you've only changed one, or how ever many you changed, the value for that particular one changes while the others stay the same (since you haven't changed those).

    I know my coding is not perfect, but it works! if you use relatively the same form, you can adapt it to whatever you need.

    Thanks for checking this out, if you have any improvements, smart assed remarks, suggestions I'm all there.

    Happy Headaches!

    Paul
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  2. #2
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps I'm missing something, or perhaps you dont have a problem at all, but I took all of your code, put it into my dev box and tested it, and it works as expected. IE: you can delete one record at a time or update as many as you wish at a time. Am I missing something?

  3. #3
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Just for clarification :)

    This is only part of the web page. This form is part of a larger set of code. You have to have a database set up, a connection, recordsets, etc.

    I'm using the latest version of php, mysql, apache server.

    The objective of this form is to either update or delete as many or as few records as you have in your table.

    I have a lot of records sitting in this one table that are to be updated by someone else. So, I made this form so they could update it on the fly as it were.

    Let me know if tht helps, I'm an aweful explainer.

    Paul
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  4. #4
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand its part of a larger application, I did not need to set up the mysql tables as I was simply outputing the SQL statements right on the page just to verify that it was working as intended. When I tested it, it seemed to me that it was doing just what you described, which is why I am unable to see what your problem / error / bug is.

  5. #5
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no problem, or error, I was posting this because I could not find a tutorial on this subject anywhere on the net!
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  6. #6
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So sorry! for some reason I thought that you were asking for help and I assumed something wasnt working right in the script

  7. #7
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool No worries mate!

    Usually when I post, I'm in desperate need of help, I'm doing my best to return the help given to me in the past! Have a good one!
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    very commendable kirikintha.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Hey spike

    Where are you from in Manchester? my uncle and aunt live in Altringham!
    Cheers
    Paul
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  10. #10
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, thank you for sharing your solution.

  11. #11
    SitePoint Member
    Join Date
    Jan 2005
    Location
    cbus
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OMG! Thank you soooo much for posting that! I've been working on a similar solution forever. An older version I was working on had to rely on a javascript and it wouldn't hold the values.

    I was able to tweak your solution to do exactly what I needed. Many many many thanks.

  12. #12
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Awesome!

    Thanks for the response, I'm working on a few other solutions to some common problems - I'll try ot post them soon!!!
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  13. #13
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have been looking for the solution of multiple update for a long time. And I can't find it until today. Thank you so much!


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
  •