Web Analytics Made Easy -
StatCounter Select/if statement issue - CodingForum

Announcement

Collapse
No announcement yet.

Select/if statement issue

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Select/if statement issue

    I can't figure out why this if/select statement isn't assigning the retrieved value to the target variable. When I run the select query directly in mysql the retrieved value is as expected. Anyone know what's wrong? Thanks.

    Code:
    if ( !isset($_POST["cover"]) ){
        	$selectDefault = sprintf("SELECT imageId FROM images WHERE id=%s ORDER BY position LIMIT 0, 1", $format->formatValue($_SESSION["id"]));     
    	$defaultResult = mysql_query($selectDefault, $connected) or die("Error: " . $selectDefault . ' ' . mysql_error());
            $default = mysql_fetch_assoc($defaultResult);
            $selectedCover = $default["imageId"];
        } else {
        	$selectedCover = $_POST["cover"];
        }

  • #2
    Where are you getting $_POST["cover"] from? Have you tried the above code manually setting the variable and seeing what happens?

    Comment


    • #3
      Originally posted by ptmuldoon View Post
      Where are you getting $_POST["cover"] from? Have you tried the above code manually setting the variable and seeing what happens?
      $_POST["cover"] is submitted from a form made-up of radio buttons. When a radio button is selected and the form submitted, $_POST["cover"] is defined and the if/else statements works fine. However, when no radio button is selected and the form is submitted, $_POST["cover"] is undefined and the SELECT statement does not fire. Lastly, when I remove the SELECT statement from the if/else block it executes correctly. Any ideas on what's going on?

      Comment


      • #4
        Post your form. I think radio button's act strangely, and that although no option was selected, the variable is still being set with a blank value.

        You may want to look into changing the if statement to look for a specific button value (ie yes or no).

        I guessing that if you reverse the if/else statement, the query works?

        See this link here, and look at the last item mentioned.

        http://www.wdvl.com/Authoring/HTML/Forms/radio.html

        If you have a bunch of RADIO buttons with the same NAME and no VALUES specified, the server won't be able to tell which is selected, since a default value of "on" will be sent.
        Last edited by ptmuldoon; Apr 14, 2009, 11:00 AM.

        Comment


        • #5
          Originally posted by ptmuldoon View Post
          Post your form. I think radio button's act strangely, and that although no option was selected, the variable is still being set with a blank value.

          You may want to look into changing the if statement to look for a specific button value (ie yes or no).

          I guessing that if you reverse the if/else statement, the query works?

          See this link here, and look at the last item mentioned.

          http://www.wdvl.com/Authoring/HTML/Forms/radio.html
          Sorry for the delayed response. To answer your guess, even if I reversed the if/else statement the query still did not work.

          However, I was able to resolve the issue by changing 3 things:

          1. Changing the radio button names from "cover[]" to "cover"
          2. Setting the ajax param value that transmits "cover" to null
          3. Changing the if statement to
          Code:
          if ( strcasecamp(ajaxValue, 'null')  == 0 ){...}
          Thanks.

          Comment

          Working...
          X