...

View Full Version : help debug sql



esthera
01-02-2006, 07:42 AM
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" />

ralph l mayo
01-02-2006, 08:10 AM
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:



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
}
}

esthera
01-02-2006, 08:42 AM
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

ralph l mayo
01-02-2006, 08:44 AM
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.


if (isset($_POST['h_scdid']) && is_array($_POST['h_scdid']))
{
foreach($_POST['h_scdid'] as $key=>$value) {
// ...

esthera
01-02-2006, 08:48 AM
also if i have this --

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

then why is the array empty?

ralph l mayo
01-02-2006, 08:57 AM
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.

esthera
01-02-2006, 09:05 AM
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?

ralph l mayo
01-02-2006, 09:16 AM
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:



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

esthera
01-02-2006, 09:19 AM
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??

ralph l mayo
01-02-2006, 09:22 AM
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.

esthera
01-02-2006, 10:22 AM
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




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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum