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 11 of 11

Thread: help debug sql

  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts

    help debug sql

    Please help me debug.

    My line of php code that is causing trouble is:
    while(list($key,$value) = each($_POST[h_scdid])){

    the error is:Warning: Variable passed to each() is not an array or object in update_shoppingcart.php on line 10

    html is
    <form action="update_shoppingcart.php" name="frmitemlist" method="post">
    <input type="hidden" name="h_scdid[]" value="128" /><input type="submit" name="btn_addtocart" value= "Update Cart" />

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    You're getting that error because before the user clicks submit and posts the data, h_scdid isn't an array or anything else. In an unrelated note, it should also be $_POST['h_scdid'] (note quotation marks).

    To fix this problem, check that $_POST is populated before your loop:

    PHP Code:
    if (isset($_POST['h_scdid']) && is_array($_POST['h_scdid']))
    {
        
    // h_scdid is present and in suitable form
        
    while(list($key,$value) = each($_POST['h_scdid']))
        {
            
    // stuff
        
    }
        
        
    // bonus functionally equivalent but more readable and faster version of the above loop
        
    foreach($_POST['h_scdid'] as $key=>$value)
        {
            
    // stuff
        
    }


  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    i'm not clear what the problem is...

    i replaced my line of code with

    foreach($_POST['h_scdid'] as $key=>$value) {

    but I get an error:

    Warning: Invalid argument supplied for foreach() in update_shoppingcart.php on line 10

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by esthera
    i'm not clear what the problem is...

    i replaced my line of code with

    foreach($_POST['h_scdid'] as $key=>$value) {

    but I get an error:

    Warning: Invalid argument supplied for foreach() in update_shoppingcart.php on line 10
    You need to check $_POST before you work with it.
    PHP Code:
    if (isset($_POST['h_scdid']) && is_array($_POST['h_scdid']))
    {
        foreach(
    $_POST['h_scdid'] as $key=>$value) {
            
    // ... 

  • #5
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    also if i have this --

    <input type="hidden" name="h_scdid[]" value="128" /><

    then why is the array empty?

  • #6
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by esthera
    also if i have this --

    <input type="hidden" name="h_scdid[]" value="128" /><

    then why is the array empty?
    because PHP doesn't know about it until the data is posted.

    If you want you can put print_r($_POST); at the top of your page to experiment with it. When you first visit the page it will show that $_POST is an empty array, and after you press the update cart button it will show that $_POST has been populated by the information in the form.

  • #7
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    but that form is posting to the php with the error ---
    i'm not clear why it's not passing through the value.

    any ideas?

  • #8
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by esthera
    but that form is posting to the php with the error ---
    i'm not clear why it's not passing through the value.

    any ideas?
    Because, like I'm trying to tell you, $_POST is empty when the page is first viewed, and not until a submit event does $_POST learn anything of the variables defined in your HTML form.

    Here's a complete example of the behavior:

    Code:
    <html>
    <body>
    <?php
    // sort($_POST['h_scdid']); // uncomment this line for a warning when visiting the page, but not after clicking the submit button.
    if (isset($_POST['h_scdid']) && is_array($_POST['h_scdid']))
    {
        echo 'h_scdid[] is set and an array: ';
        foreach($_POST['h_scdid'] as $key=>$value)
        {
            echo $key . ': ' . $value;
        }
    }
    else
    {
        echo 'Click the button to populate $_POST';
    }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="frmitemlist" method="post">
    <input type="hidden" name="h_scdid[]" value="128" /><input type="submit" name="btn_addtocart" value= "Update Cart" />
    </body>
    </html>

  • #9
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    but that's not how it is..
    my shopping cart.php has the post and calls the page with teh error with the post....

    i'm very confused.. why wouldnt' it be passing it??

  • #10
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Post all your code, I'm not sure why we're not communicating sufficiently.

    caveat: if wrapping the $_POST processing part with if (isset($_POST['h_scdid']) && is_array($_POST['h_scdid'])) { } fixes the problem I get to inflict some blunt force trauma on you.
    Last edited by ralph l mayo; 01-02-2006 at 09:38 AM.

  • #11
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    the
    if (isset($_POST['h_scdid']) && is_array($_POST['h_scdid']))

    is not executing so i guess eth h_scdid is blank.

    here's teh html
    Code:
    
     <form action="update_shoppingcart.php" name="frmitemlist" method="post">
                           <tr class="v_row">
                        
                                
                              
                               <td > $ 50.00 </td>
                               <td > <input type="text" name="txtquantity130" size="6" value ="1"  onblur = "return checkint(this);"/> <br><input type="hidden" name="h_scdid[]" value="130" /><input type="submit" name="btn_addtocart" value= "Update Cart" /></td>
                               <td > $ 50.00</td>
                            </tr>
    			</form>


  •  

    Posting Permissions

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