Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts

    Problems filling a table with multiple checkboxes

    Here's the problem I'm having. I've a table with products and a list of orders. I need to match up the products to the orders in an orginizational page.

    I've created a repeat region and a product image set that uses product images and id's to create a list. This part works just fine and it's part of the submission form. The code looks like this:
    PHP Code:
       <?php do { ?>
                  <li> <?php echo '<img src="../images/'$row_imageListRS['imageName'], '" width="172" height="120" ><br />
                     '
    $row_imageListRS['altDescription']; ?>
                    <input type="checkbox" name="imgID" value="<?php echo $row_imageListRS['id']?>" /><br />
                    <?php echo $row_imageListRS['id']; ?> </li>
                 <input type="hidden" name="userID" value="<?php echo $row_currentUserRS['userID']; ?>" />
                <input type="hidden" name="glryID" value="<?php echo $row_pageRS['id']; ?>" />
                <input type="hidden" name="MM_insert" value="form1" />
                  <?php } while ($row_imageListRS mysql_fetch_assoc($imageListRS)); ?>
    This gives me a nicely formatted list of all products with a check box for each image.

    The other information is a user ID and an order ID that also work just fine.

    I've created an insert function that should be working to populate a database table that only has 3 columns other than the primary ID. The Columns are, userID, glryID, and imgID. I want to be able to review the order and check a bunch of images from the generated list and then click the submit button and have a new row generated for each image ID.

    The problem I'm having is that the insert loop isn't looping and I'm only getting the first digit of any of the information. Here's the code for that:
    PHP Code:
      for ($i 0$len count($_POST['imgID']); $i $len$i++) {
      
    $insertSQL sprintf("INSERT INTO orderImages (userID, imgID, glryID) VALUES (%s, %s, %s)",
                           
    GetSQLValueString($_POST['userID'][$i], "int"),
                           
    GetSQLValueString($_POST['imgID'][$i], "int"),
                           
    GetSQLValueString($_POST['glryID'][$i], "int"));
      
    mysql_select_db($database_mbtseaDB$mbtseaDB);
      
    $Result1 mysql_query($insertSQL$mbtseaDB) or die(mysql_error()); 
    I can't figure out why I'm only getting one entry per submit and I can't figure out why I'm only getting the first digit.

    For example if the user ID is 93 and the image ID is 42 and the page id is 12 the resulting table row is userID 9, imgID 4, and glryID 1.

    I'm pretty sure the error is in the first line of code for the Insert method. Any help would be appreciated.
    Last edited by rgEffects; 12-08-2012 at 04:11 AM.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    $_POST['userID'][$i
    these posted items are not arrays, and posted data are initially strings. So $_POST['userID'][0] will return the first character of the string '93'.

    PHP Code:
    if (isset($_POST['userID']) && is_numeric($_POST['userID'])) {
        
    $userid intval($_POST['userid'], 10);

    but you need to resolve why you are expecting arrays from the posted data.

    BTW It only returns one row because the glryID string of '1' doesn't have a second character to retrieve.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    That explains the first character problem. Now to the other problem. I'm trying to select from dozens of check boxes individual items. Each item will be put in a table, one row per item. It's just like any shopping cart. Retrieve values from an array of items and put them together in an order with a new line in the database for each item.

    I've looked at about 20 solutions for submitting data from multiple check boxes and none of them work. Am I going about this all wrong? Retrieving a list of products from a products table, giving each a check box, then expecting that if I select 5 products I'll be able to generate 5 new rows of data in the orders table.

    For order number 10,

    product 1 Checked
    product 2 not checked
    product 3 not checked
    product 4 Checked
    product 5 Checked

    Insert records inserts these values into the table.

    order 1 ID, Product 1 ID
    order 1 ID, Product 4 ID
    order 1 ID, Product 5 ID

    Now I can call up order 1 and see all products where order ID = 1 using a repeat region method.

    If I add one product at a time this works. I need to add a bunch of products with one submit button. Any ideas would help a lot.

    Thanks

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You create an array of checkboxes by appending square brackets to the end of their name - using the same name. NB only checked items will be posted, the others will just disappear - into Santa's flight path

    Something like this:

    Code:
    <input type='checkbox' value="Product1" name='cBox[]'/>
    <input type='checkbox' value="Product2" name='cBox[]'/>
    <input type='checkbox' value="Product3" name='cBox[]'/>
    PHP Code:
    <?php
    if (isset($_POST['orderID']) && !empty($_POST['orderID'])) {
        
    $orderID $_POST['orderID'];
        
        if (isset(
    $_POST['cBox']) && !empty($_POST['cBox'])) {
            foreach (
    $_POST['cBox'] as $productID) {
                
    // SQL to insert $orderID and $productID into database
            
    }
        }
    }
    ?>
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    Thanks for the help. I'm almost there. I'm getting multiple entries but I'm getting a null in the imgID Field. I've tried a bunch of variations but I can't seem to call up the img.id. Probably going blind...

    Here's my Code:
    PHP Code:

    // Insert images into order gallery
    $editFormAction $_SERVER['PHP_SELF'];
    if (isset(
    $_SERVER['QUERY_STRING'])) {
      
    $editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
    }
    if (isset(
    $_POST['MM_insert']) && !empty($_POST['MM_insert'])) {
        
    $orderID $_POST['MM_insert'];
        
        if (isset(
    $_POST['cBox']) && !empty($_POST['cBox'])) {
            foreach (
    $_POST['cBox'] as $imgID) {
    if ((isset(
    $_POST["MM_insert"])) && ($_POST["MM_insert"] == "imageForm")) {
      
    $insertSQL sprintf("INSERT INTO glryImages (userID, imgID, glryID) VALUES (%s, %s, %s)",
                           
    GetSQLValueString($_POST['userID'], "int"),
                           
    GetSQLValueString($_POST['imgID'], "int"),
                           
    GetSQLValueString($_POST['glryID'], "int"));

      
    mysql_select_db($database_mbtseaDB$mbtseaDB);
      
    $Result1 mysql_query($insertSQL$mbtseaDB) or die(mysql_error());
    }
            }}} 
    Last edited by rgEffects; 12-09-2012 at 07:54 PM.

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    PHP Code:
    GetSQLValueString($_POST['userID'], "int"),
    GetSQLValueString($_POST['imgID'], "int"),
    GetSQLValueString($_POST['glryID'], "int")); 
    Why are you reverting to the post-data? Are these individual values? Aren't the image-ids now available as $imgID? That is, the values of the checkboxes.
    PHP Code:
    GetSQLValueString($imgID"int"), 
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    rgEffects (12-10-2012)

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Why is the following
    PHP Code:
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "imageForm")) { 
    within the foreach loop? Surely it only needs to be checked once - unless you are expecting it to be an array(?) - that is, before the foreach.

    Besides, you've already stored it as a variable $orderID. Once you've created this variable you should refer to it further down, and not keep reverting to the original posted version.
    Last edited by AndrewGSW; 12-09-2012 at 08:05 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #8
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    Thanks for helping me see what I should have seen earlier. I have made this same mistake several times before. It's so easy to define a value and then later call the definition instead of the value.

    PHP Code:
    GetSQLValueString($imgID"int"), 
    That was the fix....

    The other line was also better placed at the top of the method. I replaced the line
    PHP Code:
    if (isset($_POST['MM_insert']) && !empty($_POST['MM_insert'])) { 
    with
    PHP Code:
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "imageForm")) { 
    Now all is working just fine. My next job is to be able to delete multiple rows of a record using check boxes. That's going to also be interesting.

    Thanks for your help.

  • #9
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    My next job is to be able to delete multiple rows of a record using check boxes. That's going to also be interesting.
    Won't be that different

    My advice is to do things like this:

    PHP Code:
    if (isset($_POST['MM_insert']) && !empty($_POST['MM_insert'])) { 
        
    $MMinsert $_POST['MM_insert']; 
    once only for the post-data. That is, check it, sanitize it where needed, and store it in a variable. Then refer to this variable for the remainder of your code. It's neater and there is less chance that you will submit raw post-data into your database.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    rgEffects (12-10-2012)

  • #10
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    That's a good suggestion.. Wish I could mark this issue resolved. The option seems to have disappeared from the original post.

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by rgEffects View Post
    That's a good suggestion.. Wish I could mark this issue resolved. The option seems to have disappeared from the original post.
    If you edit your most recent post there should be an option to resolve, but I can't recall if it is in the area above or, I think, below.. It's not as obvious as it could be
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #12
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    Thanks for the suggestions


  •  

    Posting Permissions

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