SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multi Update Via Checking Box

    Hello there,
    I'm looking forward for a way on updating multi rows in my database.
    I have a table with many fields and 4 of them are ENUM fields with values YES or NO (IsActive, Index, Hot, New). If the value of "IsActive=YES' the row is active (Index=is showin in index; hot=is showing a hot image; new=is showing a new image). When i list all the rows of that db, for those field i have made a checkbox that is checked if the value is YES and unchecked if the value is NO. What i need to do now, is a way to modify multi rows of db only the field affected by checking or unchecking option and pressing a submit button.
    When i press the submit button, will be updated only rows that has any changes on it and other rows will remain intact.

    I have attached an print screen of my page to gett a better idea on what i want to do.

    Hope that someone will help me on this!

    Thank you in advance!
    Attached Images Attached Images

  2. #2
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,099
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you use IN in your SQL/mySql Where statement you are able to update multiple values. I.E.
    Code:
      UPDATE
      yourTable
      SET yourField = yourValue
      WHERE yourField IN ( 1, 3, 5, 8, 9 )
    Last edited by Mittineague; Dec 11, 2010 at 19:31. Reason: fixing bbcode tag

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by donboe View Post
    When you use IN in your SQL/mySql Where statement you are able to update multiple values. I.E.
    Code:
      UPDATE
      yourTable
      SET yourField = yourValue
      WHERE yourField IN ( 1, 3, 5, 8, 9 )
    But, since i'll have 4 fields for update which of them should be on
    Code:
    ... SET 
    yourField = yourValue
      WHERE ...
    In place of your number what should be? Field names or values?

    thank you for your help and sorry for my stupid questions

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first thing you need to do is make sure all the checkboxes for each ID in your form have the same name. Then each ID will send an array of checked checkboxes.

    The name should include the ID value to make it easier in the php script to see which ID the checkbox values belong to.

    Your php script will then receive an array of checked checkbox values for each ID.

    You then loop thorough each set of checkboxes for each ID and build your update query based on the values of the checkboxes for that ID. The value of each checkbox will tell you which columns you need to include in your update query.

    After you build an update query for each ID, then run it to update the selected columns for that ID.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    The first thing you need to do is make sure all the checkboxes for each ID in your form have the same name. Then each ID will send an array of checked checkboxes.

    The name should include the ID value to make it easier in the php script to see which ID the checkbox values belong to.

    Your php script will then receive an array of checked checkbox values for each ID.

    You then loop thorough each set of checkboxes for each ID and build your update query based on the values of the checkboxes for that ID. The value of each checkbox will tell you which columns you need to include in your update query.

    After you build an update query for each ID, then run it to update the selected columns for that ID.
    Thank you for your help, but, since i'm new in php i don't know how to do that.
    Field Names are:
    Code:
    PakIndex
    PakHot
    PakNew
    PakAktiv
    And the html list looks like this:
    HTML Code:
    <span class="result_column" style="width: 7%;"><center><input type="checkbox" style="checkbox" name="PakAktiv" value="2" /></center></span>
    <span class="result_column" style="width: 6%;"><center><input type="checkbox" style="checkbox" name="PakAktiv" value="2" /></center></span>
    <span class="result_column" style="width: 6%;"><center><input type="checkbox" style="checkbox" name="PakAktiv" value="2" checked="checked" /></center></span>
    <span class="result_column" style="width: 8%;"><center><input type="checkbox" style="checkbox" name="PakAktiv" value="2" checked="checked" /></center></span>
    Where value=ID of the row. In this case, the row have ID 2

    I have changed the type of field for this fields to TINYINT with values 1 and 0 from ENUM.
    The tablename is paketa.
    Now, having all this details, can you help me to build that query and function, PLEASE?

    Thank you in advance!

  6. #6
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to give checkboxes the same name, you do it like this

    Code:
     
    <input type="checkbox" name="chkBox_1[]" value="val_1" />
    <input type="checkbox" name="chkBox_1[]" value="val_2" />
    <input type="checkbox" name="chkBox_1[]" value="val_3" />
    <input type="checkbox" name="chkBox_1[]" value="val_4" />
    and then each checkbox that is checked will have its value sent to the server side script.

    assuming you used method="post" in your form then $_POST['chkBox_1'] in your php script will contain the checked values.

    these general coding and debugging principles might help you build your code.


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
  •