#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    448
    Rep Power
    79

    Sending array values to database in a form


    For every category that is listed with a check mark I show the "order" of the the product I am editing. i.e. The product is number "2" means it appears 2nd that category. If I have multiple categories posting it is only holding and storing a singe value. The rest are showing "0" and they should be their respective placement order number.

    Imagine a vertical list of categories with check boxes and some are checked with hidden values. Like "2" as described above.

    To simplify it, I am only showing the three sections of code where I get the value posted, then loop, and then insert to the database.

    Listing and showing the categories and check marks and values WORKS.

    What DOESN'T work is storing each value. The ' p_order' is always "0" for all entries.

    Code:
    $cats = $_REQUEST['prodCategories'];
    $orderp = $_REQUEST['orderp'];
    		
    	
    
    -------------------------------------------------------------------
    
    
    $wpdb->query("DELETE FROM category_assoc WHERE prod_id = '$lastID'");
           
    
    	for($i= 0; $i < count($cats); $i++)
    
    	{
    
    		$thecatx = $cats[$i];
    		$orderx = $orderp[$i];
    
    		$catCategory = "INSERT INTO category_assoc (prod_id, cat_id, p_order) VALUES ('$lastID', '$thecatx', '$orderx')";
    		
    		$wpdb->query($catCategory);
    		
    	}
    
    
    -----------------------------------------------------------------
    
    $cats = $prod->getProductCategories();
    				if(count($cats)>0)
    				{
    				
    					foreach($cats as $cat)
    					{
    						?>
    						<tr>
    						<td><?php echo $cat['cat_name'];?></td>
    						<td><input type='checkbox' name='prodCategories[]' value='<?php echo $cat['cat_id']; ?>' <?php if($editProduct) echo $prod->isCheckSelected($cat['cat_id'], $vals['prod_id']);?>/>
    						<?php if($editProduct) echo $prod->gettheOrder($cat['cat_id'], $vals['prod_id']); ?>
    						
    						<input type='hidden' name='orderp[]' value='<?php echo $prod->gettheOrder($cat['cat_id'], $vals['prod_id']);?>' />
    						</td>
    						</tr>
    						<?php
    					
    					}
    				}
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,350
    Rep Power
    630
    First please use the proper tags for PHP code. See the sticky at the to of this forum that says READ THIS BEFORE POSTING.

    If you are always getting 0 for 'p_order' then that means '$orderx' is always 0. Echo '$catCategory' just before the query to see what is contains. I suspect that the problem is '$orderp'. Echo that as well to verify it:
    PHP Code:
    echo "<pre>";
    print_r($orderp);
    echo 
    "</pre>"
    P.S. you should be using prepared statements rather than a direct query to limit any possible injection attack.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    448
    Rep Power
    79
    This is what the HTML is showing so I can see the value of 'oderp' is in the value. So for example the product I am editing shows in position "2" in the USA Guitars category and position "5" in the Guitars category
    Code:
    <tr>
    <td>USA Guitars</td>
    <td><input type='checkbox' name='prodCategories[]' value='3' checked/>
    												
    <input type="hidden" name="orderp[]" value='2'/>
    </td>
    </tr>
    <tr>
    <td>Guitars</td>
    <td><input type='checkbox' name='prodCategories[]' value='36' checked />
    												
    <input type="hidden" name="orderp[]" value='5'/>
    </td>
    </tr>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,350
    Rep Power
    630
    That has nothing to do with what is being passed to the database in the query. If you add the debug as I suggested you should be able to figure out where it is going wrong.

    P.S. Don't use $_REQUEST use $_POST instead.
    Last edited by gw1500se; October 18th, 2017 at 02:55 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,814
    Rep Power
    4536
    Start working the logic back from the query and echo'ing values to see where you're losing the $orderp value. Or start echo'ing from $_REQUEST and go the other way to see where you lose it. At some point, you must because the basic logic appears fine.

    PHP Code:
    <?php
    $_KINDAREQUEST
    ['prodCats'] = array('one','two');
    $_KINDAREQUEST['orderp'] = array('2','5');

    $cats $_KINDAREQUEST['prodCats'];
    $orderp $_KINDAREQUEST['orderp'];

    for(
    $i 0$i count($cats); $i++) {
        
    $catx $cats[$i];
        
    $orderx $orderp[$i];
        
    $query "INSERT INTO table ( ... ) VALUES ('','$catx','$orderx')";
        
        echo 
    $query "\n";
    }
    OUTPUT:
    INSERT INTO table ( ... ) VALUES ('','one','2')
    INSERT INTO table ( ... ) VALUES ('','two','5')
    -- Cigars, whiskey and wild, wild women. --
  10. #6
  11. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,814
    Rep Power
    4536
    Wait, if you're using checkboxes and hidden values, the keys aren't likely to line up. If only three boxes are checked, the array will only have elements 0 - 2. but the hidden elements with arrays will have as many as were on the page. And there'll be no way to relate the two.

    Instead of naming elements with [], you'll want to explicitly give the keys there so prodCat[3] and orderp[3] are relating to the same thing.
    -- Cigars, whiskey and wild, wild women. --
  12. #7
  13. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,814
    Rep Power
    4536
    Checkbox values are only sent if the box is checked. Not sure if that was clear.

    Comments on this post

    • mallen agrees
    -- Cigars, whiskey and wild, wild women. --
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    448
    Rep Power
    79
    I got it solved.
    Code:
    <input type="hidden" name="ords[]" <?php if($editProduct) echo "value='{$cat['p_order']}'"; ?>/>
    	
    ---------------------------------------------
    	
    $ords =	$_REQUEST['ords'];
    		
    $catCategory = "INSERT INTO category_assoc (prod_id, cat_id, p_order) VALUES('$lastID', '". $cats[$i] . "','". $ords[$i] . "')";
    	
    ---------------------------------------------
    	
    I think the quote marks were an issue. Also these two parts. 
    $ords[$i]
    name="ords[]"

IMN logo majestic logo threadwatch logo seochat tools logo