14 Replies Latest reply on Nov 11, 2007 6:37 PM by (Steve_Guluk)

    PHP Insert record logic

    Level 1
      Hello,
      I'm coming up blank on the basic logic to insert a record based on results gathered to a form page. Inserting txt field entry items are are no problem but I have a displayed value created by polling another table, collecting the results, then passes the results to the form page and if there are no results displays a message in place of "the results".

      I now want to simply (or obviously not for me) include "the results" as one of the values I am inserting from the form as a new record.

      Using this code I gather my list of Models selected:
      Code:

      echo ("No models selected") ; // Show if recordset empty
      } else {
      do {
      echo $row_rsCookModels [ 'model_name' ] ;
      echo (", ") ;

      } while ( $row_rsCart = mysql_fetch_assoc ($rsCookModels)) ;
      }
      ?>

      I insert a comma between each entry found and then write the results to the form page.

      At this point how do I gather these results to then insert in a new visitor record in a different table?

      Do I turn the result into a variable and then insert it into a hidden field then use "that" to insert as a new record to the visitor table?

      Would I be creating two results in my code posted above? One value to display on screen and one to use as a saved string (var) to insert as a new record?

      I'll be glad to figure out the code I just need to understand the "logic" of how best to achieve this goal.

      Any advice would be appreciated.
        • 1. Re: PHP Insert record logic
          Level 1
          Seems like the answer might be to save the results string to a var then post that to a hidden field for insertion on POST.

          Yes?
          • 2. Re: PHP Insert record logic
            Günter Schenk Level 4
            Hi Steve,

            way back then I posted a similar how-to in the Interakt forums -- mayb this is what you´re after:

            http://www.interaktonline.com/Products/Dreamweaver-Extensions/MXSendE-mail/Product-Forum/D etails/76038/howto+%3A%3A+store+recipients+data+as+comma+separated+values.html

            the main trick here is to apply a horizontal looper to a hidden field´s "value"

            Cheers,
            Günter Schenk
            Adobe Community Expert, Dreamweaver
            • 3. Re: PHP Insert record logic
              Level 1
              Thanks Günter,
              But after reading your article I see that it "looks" to do what I already have achieved which is displaying the results and separating them with a comma. I have no problems with that part, nor displaying a message IF there are no items in the cart table related to that user.

              Those results are what I then want to insert into the same Visitor table when the other txt fields are used to also insert a new record.

              My English must be terrible or I need to learn some new means to describe what I am looking to achieve. It's probably too easy so people think I am looking for something else?
              • 4. Re: PHP Insert record logic
                Günter Schenk Level 4
                Hi Steve,

                well, that might have rather been a misunderstanding on my behalf, so let´s just ask -- on your Insert Record page, you want to e.g. populate a hidden field with some values (e.g. from a checkbox array) gathered from a regular form where folks select one or more products ? Or are the values you´re talking about meant to be gathered on the very same page ?

                Cheers,
                Günter Schenk
                Adobe Community Expert, Dreamweaver
                • 5. Re: PHP Insert record logic
                  Level 1
                  Yes, the values are gathered and displayed on the page already using the code noted above. It outputs the selected items, comma separated. I would like to then take that value as a string and be able to insert it when I insert the txt field values as a new record in a totally non dependent field.

                  This is my form page that works with or without selected models:
                  http://retailertoolkit.com/adinforequest.php

                  This page is where models are able to be selected :
                  http://retailertoolkit.com/inventory.php

                  I like the idea of capturing the IF and or ELSE IF values as it will insert the "No models selected" text into the new record just the same as if there were a string of models. Nice and clean. I just need the logic to take the values displayed and be able to insert them when I pass the form values as a new record, saving the visitors choices and personal information.
                  • 6. Re: PHP Insert record logic
                    Günter Schenk Level 4
                    Hi Steve, <br /> <br />guess I now understand what you´re really after, lol :-) <br /> <br />If you want the displayed comma separated values to be additionally inserted, you´re basically on the right track with using a hidden field for this. <br /> <br />My suggestion: <br /> <br />1. whatever field initially has a predefined "value" such as <?php echo KT_escapeAttribute($row_rsgarment_specs['name']); ?> -- that´s what I would delete in order to make room for... <br /> <br />2. applying the "do while" loop to the hidden field´s empty value: <br /> <br />value="<?php<br />do { echo $row_rsCookModels [ 'model_name' ] ; echo (", ") ;<br />}<br />while ( $row_rsCart = mysql_fetch_assoc ($rsCookModels)) ;<br />}<br />?>" <br /> <br />Would that help ? <br /> <br />Cheers, <br />Günter Schenk <br />Adobe Community Expert, Dreamweaver
                    • 7. Re: PHP Insert record logic
                      Level 1
                      Thanks Günter ,
                      Yes so you are saying that

                      value="
                      is used to create the var called "value"?

                      And the code would be PHP in PHP?

                      $value = "
                      ?>

                      Then of course the passing of that var to the field that is used to update ( I understand that part).

                      Is this what you are saying? Establish a var based on the results of the loop and then use that to display on page as well as pass via form doing the insert?
                      • 8. Re: PHP Insert record logic
                        Günter Schenk Level 4
                        Hi Steve,

                        no, "value" is meant to be the standard attribute of any regular form element, e.g. input type="hidden" value ="" -- and a copy of that PHP loop you´re already using for displaying the seleted values can be embedded in the hidden field´s "value" attribute.

                        ------
                        And the code would be PHP in PHP?
                        ------

                        you can´t have such PHP in PHP constructs -- that will lead to errors

                        -------
                        Establish a var based on the results of the loop
                        -------

                        your don´t need to establish an extra var -- just use the loop inside the existing hidden field´s value to populate it.

                        I´m attaching a text-only example of what I´m talking about

                        Cheers,
                        Günter Schenk
                        Adobe Community Expert, Dreamweaver
                        • 9. Re: PHP Insert record logic
                          Level 1
                          :0 :)

                          So you're saying I can run the whole PHP loop inside a hidden tag "value" attribute.

                          That's great! I understand fully. I tested it and it works as explained.

                          Very cool aspect of PHP.

                          Thanks very much!!
                          • 10. Re: PHP Insert record logic
                            Günter Schenk Level 4
                            Hi Steve,

                            see ? :-) :-)

                            Cheers,
                            Günter Schenk
                            Adobe Community Expert, Dreamweaver
                            • 11. Re: PHP Insert record logic
                              Level 1
                              I now see... Thanks again!
                              • 12. Re: PHP Insert record logic
                                Level 1
                                things grow cloudy....<br /><br />Seems I can only use the method once. IF used in the value of a text field it works:<br /><br /> <input type="text" name="selectedModels" id="selectedModels" value="<?php <br />       if ($totalRows_rsCookModels == 0) {<br />          echo ("No models selected") ; <br />} else { <br />do { <br />                    echo $row_rsCookModels['model_name']; <br />                    echo (", ") ;<br />                     } while ($row_rsCookModels = mysql_fetch_assoc($rsCookModels)); }?>"><br /><br />But then the later same code used to display the results to screen fails:<br /><br /> <?php if ($totalRows_rsCookModels == 0) {<br />          echo ("No models selected") ; // Show if recordset empty <br />} else {<br />          do {<br />     echo $row_rsCookModels['model_name'];<br />     echo (", ") ;<br />} while ($row_rsCookModels = mysql_fetch_assoc($rsCookModels));<br />          }<br />?><br /><br />But that same latter code works when the form field code is removed.<br /><br />Is there something that is wiping out some available value? Does the recordset expire after use? I'm sure I could create a different named record set but that seems to be less efficient.<br /><br />Any idea what new logic I need to grasp to understand the problem? Again should I be creating a var value of the string so t can be used multiple times in the same page? Is that the logic that escapes me? Us?
                                • 13. Re: PHP Insert record logic
                                  Günter Schenk Level 4
                                  Hi Steve,

                                  yes indeed, I recall to have encountered similar issues when reusing the same recordset for more than one loop -- though it´s certainly inefficient, please try with a differently named recordset that´s otherwise containing the same query etc, and use that for the second loop.

                                  works better now ?

                                  Cheers,
                                  Günter Schenk
                                  Adobe Community Expert, Dreamweaver
                                  • 14. Re: PHP Insert record logic
                                    Level 1
                                    Hello Günter ,
                                    I saw that asp ADO pages could use some cloning of a recordset. I guess this is the purpose, to keep a recordset alive.

                                    I duplicated the recordset and changed the name and all works fine.

                                    Thanks for your help,

                                    Steve