$_POST variable problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack

    $_POST variable problem

    How would I add a variable that I will assign to a list of $_POST
    variables that I extract from a form?

    My form passes a value for $q. That works fine. What I want to do is run
    an if/else on it and assign a new variable based on what was chosen such as

    if ($q == "red") {
    $q = "tp"; //change $q to this value
    $sub = "ak"; //attach this value to the $sub variable
    } else {
    $q = $q; //don't change the value of $q
    $sub = ""; //Leave $sub empty
    }

    Then both the $q and $sub will get added to a mysql_query for insertion.

    mysql_query("IN SERT INTO `table` VALUES(\"\",\"$ q\",\"$sub\",\" $comments
    - $name\",\"$emai l\",\"$today\", \"\" )") or die(mysql_error ());

    I can get it to add the $q with no problem but it isn't recognizing the
    $sub that wasn't part of the original $_POST variables. Any suggestions?

  • CJ Llewellyn

    #2
    Re: $_POST variable problem

    "Jack" <nospam@noemail .com> wrote in message
    news:KcHDc.1628 74$3x.32326@att bi_s54...[color=blue]
    > How would I add a variable that I will assign to a list of $_POST
    > variables that I extract from a form?
    >
    > My form passes a value for $q. That works fine. What I want to do is run
    > an if/else on it and assign a new variable based on what was chosen such[/color]
    as[color=blue]
    >
    > if ($q == "red") {
    > $q = "tp"; //change $q to this value
    > $sub = "ak"; //attach this value to the $sub variable
    > } else {
    > $q = $q; //don't change the value of $q
    > $sub = ""; //Leave $sub empty
    > }
    >
    > Then both the $q and $sub will get added to a mysql_query for insertion.
    >
    > mysql_query("IN SERT INTO `table` VALUES(\"\",\"$ q\",\"$sub\",\" $comments
    > - $name\",\"$emai l\",\"$today\", \"\" )") or die(mysql_error ());[/color]

    It'd help if you wrote your code so you could easily see what was going
    wrong. Dying on a database error is extremely user unfriendly.

    $sql = "INSERT INTO `table` VALUES(\"\",\"$ q\",\"$sub\",\" $comments -
    $name\",\"$emai l\",\"$today\", \"\" )";
    $result = mysql_query($sq l , $conn);
    if(! $result || mysql_error($co nn))
    {
    echo $sql . "<br>\n";
    echo mysql_error($co nn);
    exit;
    }

    Using this type of structure will allow you to control the fallout of the
    error rather than just providing an unintelligable error message.

    Write INSERT statements in the format

    INSERT INTO tablename (field1 , field2, field3 ...) VALUES
    ('value1','valu e2','value3' ..)

    Furthermore, mysql and postgresql use ' as the field delimiter, wheres MS
    SQL and MS Access use " as the field delimiter.



    Comment

    • Jack

      #3
      Re: $_POST variable problem

      CJ Llewellyn wrote:[color=blue]
      > "Jack" <nospam@noemail .com> wrote in message
      > news:KcHDc.1628 74$3x.32326@att bi_s54...
      >[color=green]
      >>How would I add a variable that I will assign to a list of $_POST
      >>variables that I extract from a form?
      >>
      >>My form passes a value for $q. That works fine. What I want to do is run
      >>an if/else on it and assign a new variable based on what was chosen such[/color]
      >
      > as
      >[color=green]
      >>if ($q == "red") {
      >>$q = "tp"; //change $q to this value
      >>$sub = "ak"; //attach this value to the $sub variable
      >>} else {
      >>$q = $q; //don't change the value of $q
      >>$sub = ""; //Leave $sub empty
      >>}
      >>
      >>Then both the $q and $sub will get added to a mysql_query for insertion.
      >>
      >>mysql_query(" INSERT INTO `table` VALUES(\"\",\"$ q\",\"$sub\",\" $comments
      >>- $name\",\"$emai l\",\"$today\", \"\" )") or die(mysql_error ());[/color]
      >
      >
      > It'd help if you wrote your code so you could easily see what was going
      > wrong. Dying on a database error is extremely user unfriendly.
      >
      > $sql = "INSERT INTO `table` VALUES(\"\",\"$ q\",\"$sub\",\" $comments -
      > $name\",\"$emai l\",\"$today\", \"\" )";
      > $result = mysql_query($sq l , $conn);
      > if(! $result || mysql_error($co nn))
      > {
      > echo $sql . "<br>\n";
      > echo mysql_error($co nn);
      > exit;
      > }
      >
      > Using this type of structure will allow you to control the fallout of the
      > error rather than just providing an unintelligable error message.
      >
      > Write INSERT statements in the format
      >
      > INSERT INTO tablename (field1 , field2, field3 ...) VALUES
      > ('value1','valu e2','value3' ..)
      >
      > Furthermore, mysql and postgresql use ' as the field delimiter, wheres MS
      > SQL and MS Access use " as the field delimiter.
      >[/color]

      Thank you CJ. I changed my mysql code as you recommended. However the
      problem remains. As bad as my code was, it worked. It inserted the
      information into the database. And there's no error message generated
      either with my old code or with your cleaned up code. It just doesn't
      see the $sub category at all. Any other ideas?

      Comment

      • CJ Llewellyn

        #4
        Re: $_POST variable problem

        "Jack" <nospam@noemail .com> wrote in message
        news:uzIDc.1256 98$Sw.55866@att bi_s51...
        -snip-[color=blue]
        > Thank you CJ. I changed my mysql code as you recommended. However the
        > problem remains. As bad as my code was, it worked. It inserted the
        > information into the database. And there's no error message generated
        > either with my old code or with your cleaned up code. It just doesn't
        > see the $sub category at all. Any other ideas?[/color]

        Remove the row from the table and add this before the mysql_query line:-

        echo "[" & $q & "]<br>\n";
        if(! isset($sub))
        echo '$sub is not set!<br>';



        Comment

        • Jack

          #5
          Re: $_POST variable problem

          CJ Llewellyn wrote:
          [color=blue]
          > "Jack" <nospam@noemail .com> wrote in message
          > news:uzIDc.1256 98$Sw.55866@att bi_s51...
          > -snip-
          >[color=green]
          >>Thank you CJ. I changed my mysql code as you recommended. However the
          >>problem remains. As bad as my code was, it worked. It inserted the
          >>information into the database. And there's no error message generated
          >>either with my old code or with your cleaned up code. It just doesn't
          >>see the $sub category at all. Any other ideas?[/color]
          >
          >
          > Remove the row from the table and add this before the mysql_query line:-
          >
          > echo "[" & $q & "]<br>\n";
          > if(! isset($sub))
          > echo '$sub is not set!<br>';
          >[/color]

          As one would expect, it returns $sub is not set!

          Now back to my original question:

          if ($q == "red") {
          $q = "tp"; //change $q to this value
          $sub = "ak"; //attach this value to the $sub variable
          } else {
          $q = $q; //don't change the value of $q
          $sub = ""; //Leave $sub empty
          }

          I can get it to recognize the $q from the $_POST array with no problem
          but it isn't recognizing the $sub that wasn't part of the original
          $_POST variables. Is there some problem with my trying to set the $sub
          variable with the above if statement? If so, what is the correct way to
          set that variable manually?

          Comment

          • FLEB

            #6
            Re: $_POST variable problem

            Regarding this well-known quote, often attributed to Jack's famous "Mon, 28
            Jun 2004 00:35:08 GMT" speech:
            [color=blue]
            > CJ Llewellyn wrote:
            >[color=green]
            >> "Jack" <nospam@noemail .com> wrote in message
            >> news:uzIDc.1256 98$Sw.55866@att bi_s51...
            >> -snip-
            >>[color=darkred]
            >>>Thank you CJ. I changed my mysql code as you recommended. However the
            >>>problem remains. As bad as my code was, it worked. It inserted the
            >>>informatio n into the database. And there's no error message generated
            >>>either with my old code or with your cleaned up code. It just doesn't
            >>>see the $sub category at all. Any other ideas?[/color]
            >>
            >>
            >> Remove the row from the table and add this before the mysql_query line:-
            >>
            >> echo "[" & $q & "]<br>\n";
            >> if(! isset($sub))
            >> echo '$sub is not set!<br>';
            >>[/color]
            >
            > As one would expect, it returns $sub is not set!
            >
            > Now back to my original question:
            >
            > if ($q == "red") {
            > $q = "tp"; //change $q to this value
            > $sub = "ak"; //attach this value to the $sub variable
            > } else {
            > $q = $q; //don't change the value of $q
            > $sub = ""; //Leave $sub empty
            > }
            >
            > I can get it to recognize the $q from the $_POST array with no problem
            > but it isn't recognizing the $sub that wasn't part of the original
            > $_POST variables. Is there some problem with my trying to set the $sub
            > variable with the above if statement? If so, what is the correct way to
            > set that variable manually?[/color]

            First off, your code would be easier to sort out, as well as more portable
            and secure, using the $_POST superglobal, instead of just the
            register_global s automation. So, with that:

            <?php
            if ($_POST['q'] == "red") {
            $q = "tp"; //change $q to this value
            $sub = "ak"; //attach this value to the $sub variable
            } else {
            $q = $_POST['q']; // assign $q to the POSTed value
            $sub = ""; // Leave $sub empty
            }
            ?>

            That said, have you tried some rough debugging?

            <?php
            echo "POST value of q = '" . $_POST['q'] . "'<br>\n";

            if ($_POST['q'] == "red") {
            $q = "tp"; //change $q to this value
            $sub = "ak"; //attach this value to the $sub variable
            echo "Posted q was 'red'. Now \$q is $q and \$sub is $sub<br>\n";
            } else {
            $q = $_POST['q']; // assign $q to the POSTed value
            $sub = ""; // Leave $sub empty
            echo "Posted q was NOT 'red'. Now \$q is $q and \$sub is $sub<br>\n";
            }

            echo "FINAL RESULT: \$q = '$q', \$sub = $sub";
            ?>

            See what you come up with there. If you find that it's not detecting the
            "redness", there might be extraneous characters getting passed in the POST.
            Try perhaps using a strpos() search, a preg_match() search, or even just
            trim()-ming and rtrim()-ming the posted value before you check it.

            --
            -- Rudy Fleminger
            -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
            (put "Hey!" in the Subject line for priority processing!)
            -- http://www.pixelsaredead.com

            Comment

            • FLEB

              #7
              Re: $_POST variable problem

              Regarding this well-known quote, often attributed to Jack's famous "Mon, 28
              Jun 2004 00:35:08 GMT" speech:
              [color=blue]
              > CJ Llewellyn wrote:
              >[color=green]
              >> "Jack" <nospam@noemail .com> wrote in message
              >> news:uzIDc.1256 98$Sw.55866@att bi_s51...
              >> -snip-
              >>[color=darkred]
              >>>Thank you CJ. I changed my mysql code as you recommended. However the
              >>>problem remains. As bad as my code was, it worked. It inserted the
              >>>informatio n into the database. And there's no error message generated
              >>>either with my old code or with your cleaned up code. It just doesn't
              >>>see the $sub category at all. Any other ideas?[/color]
              >>
              >>
              >> Remove the row from the table and add this before the mysql_query line:-
              >>
              >> echo "[" & $q & "]<br>\n";
              >> if(! isset($sub))
              >> echo '$sub is not set!<br>';
              >>[/color]
              >
              > As one would expect, it returns $sub is not set!
              >
              > Now back to my original question:
              >
              > if ($q == "red") {
              > $q = "tp"; //change $q to this value
              > $sub = "ak"; //attach this value to the $sub variable
              > } else {
              > $q = $q; //don't change the value of $q
              > $sub = ""; //Leave $sub empty
              > }
              >
              > I can get it to recognize the $q from the $_POST array with no problem
              > but it isn't recognizing the $sub that wasn't part of the original
              > $_POST variables. Is there some problem with my trying to set the $sub
              > variable with the above if statement? If so, what is the correct way to
              > set that variable manually?[/color]

              First off, your code would be easier to sort out, as well as more portable
              and secure, using the $_POST superglobal, instead of just the
              register_global s automation. So, with that:

              <?php
              if ($_POST['q'] == "red") {
              $q = "tp"; //change $q to this value
              $sub = "ak"; //attach this value to the $sub variable
              } else {
              $q = $_POST['q']; // assign $q to the POSTed value
              $sub = ""; // Leave $sub empty
              }
              ?>

              That said, have you tried some rough debugging?

              <?php
              echo "POST value of q = '" . $_POST['q'] . "'<br>\n";

              if ($_POST['q'] == "red") {
              $q = "tp"; //change $q to this value
              $sub = "ak"; //attach this value to the $sub variable
              echo "Posted q was 'red'. Now \$q is $q and \$sub is $sub<br>\n";
              } else {
              $q = $_POST['q']; // assign $q to the POSTed value
              $sub = ""; // Leave $sub empty
              echo "Posted q was NOT 'red'. Now \$q is $q and \$sub is $sub<br>\n";
              }

              echo "FINAL RESULT: \$q = '$q', \$sub = $sub";
              ?>

              See what you come up with there. If you find that it's not detecting the
              "redness", there might be extraneous characters getting passed in the POST.
              Try perhaps using a strpos() search, a preg_match() search, or even just
              trim()-ming and rtrim()-ming the posted value before you check it.

              --
              -- Rudy Fleminger
              -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
              (put "Hey!" in the Subject line for priority processing!)
              -- http://www.pixelsaredead.com

              Comment

              • CJ Llewellyn

                #8
                Re: $_POST variable problem

                "Jack" <nospam@noemail .com> wrote in message
                news:0JJDc.1635 35$3x.4031@attb i_s54...[color=blue]
                > CJ Llewellyn wrote:
                >[color=green]
                > > "Jack" <nospam@noemail .com> wrote in message
                > > news:uzIDc.1256 98$Sw.55866@att bi_s51...
                > > -snip-
                > >[color=darkred]
                > >>Thank you CJ. I changed my mysql code as you recommended. However the
                > >>problem remains. As bad as my code was, it worked. It inserted the
                > >>information into the database. And there's no error message generated
                > >>either with my old code or with your cleaned up code. It just doesn't
                > >>see the $sub category at all. Any other ideas?[/color]
                > >
                > >
                > > Remove the row from the table and add this before the mysql_query line:-
                > >
                > > echo "[" & $q & "]<br>\n";
                > > if(! isset($sub))
                > > echo '$sub is not set!<br>';
                > >[/color]
                >
                > As one would expect, it returns $sub is not set!
                >
                > Now back to my original question:
                >
                > if ($q == "red") {
                > $q = "tp"; //change $q to this value
                > $sub = "ak"; //attach this value to the $sub variable
                > } else {
                > $q = $q; //don't change the value of $q
                > $sub = ""; //Leave $sub empty
                > }
                >
                > I can get it to recognize the $q from the $_POST array with no problem
                > but it isn't recognizing the $sub that wasn't part of the original
                > $_POST variables. Is there some problem with my trying to set the $sub
                > variable with the above if statement? If so, what is the correct way to
                > set that variable manually?[/color]

                $sub = "ak"; as per your statement. You didn't say what $q contained.
                Furthmore your else condition is redundent.

                Try :-

                $sub = '';
                if($q == "red")
                {
                echo "q was red";
                $q = "tp";
                $sub = "ak";
                }


                Comment

                • Jack

                  #9
                  Re: $_POST variable problem

                  CJ Llewellyn wrote:[color=blue]
                  > "Jack" <nospam@noemail .com> wrote in message
                  > news:0JJDc.1635 35$3x.4031@attb i_s54...
                  >[color=green]
                  >>CJ Llewellyn wrote:
                  >>
                  >>[color=darkred]
                  >>>"Jack" <nospam@noemail .com> wrote in message
                  >>>news:uzIDc.1 25698$Sw.55866@ attbi_s51...
                  >>>-snip-
                  >>>
                  >>>Remove the row from the table and add this before the mysql_query line:-
                  >>>
                  >>>echo "[" & $q & "]<br>\n";
                  >>>if(! isset($sub))
                  >>> echo '$sub is not set!<br>';
                  >>>[/color]
                  >>
                  >>As one would expect, it returns $sub is not set!
                  >>
                  >>Now back to my original question:
                  >>
                  >>if ($q == "red") {
                  >> $q = "tp"; //change $q to this value
                  >> $sub = "ak"; //attach this value to the $sub variable
                  >>} else {
                  >> $q = $q; //don't change the value of $q
                  >> $sub = ""; //Leave $sub empty
                  >>}
                  >>
                  >>I can get it to recognize the $q from the $_POST array with no problem
                  >>but it isn't recognizing the $sub that wasn't part of the original
                  >>$_POST variables. Is there some problem with my trying to set the $sub
                  >>variable with the above if statement? If so, what is the correct way to
                  >>set that variable manually?[/color]
                  >
                  >
                  > $sub = "ak"; as per your statement. You didn't say what $q contained.
                  > Furthmore your else condition is redundent.[/color]

                  And therin lied my problem. There was another if/else a little further
                  on that was resetting the $sub to ''. Removing the else statements
                  solved the problem.
                  [color=blue]
                  > Try :-
                  >
                  > $sub = '';
                  > if($q == "red")
                  > {
                  > echo "q was red";
                  > $q = "tp";
                  > $sub = "ak";
                  > }
                  >[/color]


                  It was a variation of your suggestion here that helped me find the
                  problem. By adding several of these in different parts of the script and
                  exiting before the mysql query, I narrowed down where the problem was
                  coming from.

                  It now works as intended. Thank you so much for your patience and
                  assitsance.

                  Comment

                  • Jack

                    #10
                    Re: $_POST variable problem

                    FLEB wrote:
                    [color=blue]
                    > Regarding this well-known quote, often attributed to Jack's famous "Mon, 28
                    > Jun 2004 00:35:08 GMT" speech:
                    >[color=green]
                    >>
                    >>if ($q == "red") {
                    >> $q = "tp"; //change $q to this value
                    >> $sub = "ak"; //attach this value to the $sub variable
                    >>} else {
                    >> $q = $q; //don't change the value of $q
                    >> $sub = ""; //Leave $sub empty
                    >>}
                    >>
                    >>I can get it to recognize the $q from the $_POST array with no problem
                    >>but it isn't recognizing the $sub that wasn't part of the original
                    >>$_POST variables. Is there some problem with my trying to set the $sub
                    >>variable with the above if statement? If so, what is the correct way to
                    >>set that variable manually?[/color]
                    >
                    >
                    > First off, your code would be easier to sort out, as well as more portable
                    > and secure, using the $_POST superglobal, instead of just the
                    > register_global s automation. So, with that:
                    >
                    > <?php
                    > if ($_POST['q'] == "red") {
                    > $q = "tp"; //change $q to this value
                    > $sub = "ak"; //attach this value to the $sub variable
                    > } else {
                    > $q = $_POST['q']; // assign $q to the POSTed value
                    > $sub = ""; // Leave $sub empty
                    > }
                    > ?>[/color]

                    I had previously extracted the $_POST value of $q before running the if
                    statement. The value red was being properly passed all along. I
                    understand what your saying with your example and would definitely use
                    it had the $q value not already been extracted.

                    [color=blue]
                    > That said, have you tried some rough debugging?
                    >
                    > <?php
                    > echo "POST value of q = '" . $_POST['q'] . "'<br>\n";
                    >
                    > if ($_POST['q'] == "red") {
                    > $q = "tp"; //change $q to this value
                    > $sub = "ak"; //attach this value to the $sub variable
                    > echo "Posted q was 'red'. Now \$q is $q and \$sub is $sub<br>\n";
                    > } else {
                    > $q = $_POST['q']; // assign $q to the POSTed value
                    > $sub = ""; // Leave $sub empty
                    > echo "Posted q was NOT 'red'. Now \$q is $q and \$sub is $sub<br>\n";
                    > }
                    >
                    > echo "FINAL RESULT: \$q = '$q', \$sub = $sub";
                    > ?>
                    >
                    > See what you come up with there. If you find that it's not detecting the
                    > "redness", there might be extraneous characters getting passed in the POST.
                    > Try perhaps using a strpos() search, a preg_match() search, or even just
                    > trim()-ming and rtrim()-ming the posted value before you check it.[/color]

                    It was a variation of the code such as you posted that allowed me to
                    find that the problem was being caused by a second if/else statement
                    that was resetting the $sub value to ''. Eliminating both else portions
                    rectified the problem.

                    Thank you for your suggestions. Much obliged.

                    Comment

                    • Virgil Green

                      #11
                      Re: $_POST variable problem

                      "Jack" <nospam@noemail .com> wrote in message
                      news:WxUDc.1237 48$HG.47924@att bi_s53...[color=blue]
                      > And therin lied my problem. There was another if/else a little further
                      > on that was resetting the $sub to ''. Removing the else statements
                      > solved the problem.
                      >[/color]

                      And this is why you (and all others ) are encouraged to post small, complete
                      examples demonstrating any perceived problem. Often, the simple act of
                      preparing the example will lead you to the source of the bug.

                      - Virgil


                      Comment

                      Working...