SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Multiple checkboxes, update DB, and IN() - advice needed.

    I'm doing some research on multiple checkboxes that update multiple records in a DB. That is... first list all the rows from table/tables that match the SELECT query. Use PHP to output form with checkbox instead of value for one column. Then user checks one or several boxes, and hits "Submit". The data (array) is then sent to the DB through a function.

    So... I think I have the output of the checkbox-part of the form correct.
    PHP Code:
    <?php
    echo '<input type="checkbox" name="voided[]" value="'.$row['invoice_no'].'">'
    ?>
    Please correct me if I'm wrong. The background for the entire HTML-table is values from two MySQL-tables, and is dynamically created.

    Now... the UPDATE query... I've seen two ways of doing this, but if I understand correctly, the best aproach is the IN() way.

    Example taken from a forum...
    PHP Code:
    if (count($del)) { 
    $str implode(",",$del); 
    $result mysql_query("DELETE FROM posts WHERE id IN($str)" ); 
      } 
    Now I was thinking of something along the lines of...
    UPDATE invoices SET voided=1 WHERE invoice_no IN()
    instead of the delete in the above example. I'm just not really sure on how to put it together. There isn't much information about IN() at mysql.com, but what I found confirmed its usabillity.

    One other thing... is it really true that non-checked checkboxes are ignored when submitting a form? I'm using 1 as signifying marked/checked/true and 0 or blank as not checked/false. (Seemed logical at the time). I have tried checkboxes for changing from 1 to 0 and back, so that part is working. Not pretty, but working. One checkbox - one form.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  2. #2
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes non checkedboxes are ignored so what you will do is to check if it is an array first before updating/deleting
    is_array($fieldname)

    if it is array then implode(",", $chkBoxes) and put the value into you IN()

  3. #3
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks let me try it and see how it goes...
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  4. #4
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok tried it and it worked

    The key to it is to make sure the checkbox looks like this:
    PHP Code:
    echo '<input type="checkbox" name="voided[]" value="'.$row['invoice_no'].'">'
    This is the part from the query-function void_invoices($invoices):
    PHP Code:
    $query "update invoices set void=1 where invoice_no in($invoices)"
    And this is what the form calls:
    PHP Code:
    $voided $_POST['voided'];

    if(
    is_array($voided))
        {
            
    $invoices implode(",",$voided);
            
    void_invoice($invoices);
        } 
    Hope someone finds this useful
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...


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
  •