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 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    73
    Thanks
    5
    Thanked 9 Times in 9 Posts

    Question Why does script create 2 records?

    I don't use PHP if I can help it but in some cases I have too, in this case I needed a form to capture some information, wrap the results in some additional HTML and Microformats coding and then append it to a file so i could use PHP Include.

    This took a lot of searching and testing for me to pull it off (Don't know PHP very well), but anyways here is the problem - When I submit the form it makes 2 records in the file, one that is blank (no form data) and the second record which has the proper information. Other than creating two records it works properly, what do i need to change for this to create only the record with the form data?

    Note: My first thoughts were this:action="<?php echo $PHP_SELF;?>" was the cause of the duplication, but if that is the cause i don't know what to change it to for it to work properly.

    PHP Code:
    <?php
    $Rname 
    $_POST["rname"];
    $Rate $_POST["rating"];
    $Title $_POST["title"];
    $Sumr $_POST["summary"];
    $mnth $_POST["month"];
    $day $_POST["day"];
    $year $_POST["year"];
    ?>

    <form id="form1" method="post" action="<?php echo $PHP_SELF;?>">
      Reviewers Name:<input name="rname" type="text" size="30" maxlength="30">
      <br>
      <div xmlns="http://www.w3.org/1999/xhtml" class="field">
              <label for="rating">rating</label>
              <select id="rating" name="rating">
                <option/>
                <option label="★" value="1">1</option>
                <option label="★★" value="2">2</option>
                <option label="★★★" value="3">3</option>

                <option label="★★★★" value="4">4</option>
                <option label="★★★★★" value="5">5</option>
              </select>
              <label id="summary-label" for="title">Title</label>
              <input type="text" id="title" name="title"/><br>
              <label id="summary-label" for="title">Review</label>
              <textarea rows="10" cols="30" name="summary" id="summary"></textarea><br>
              <label for="review-date-month">review date</label>
              <select id="review-date-month" name="month">
                <option value="01">Jan</option>
                <option value="02">Feb</option>
                <option value="03">Mar</option>
                <option value="04">Apr</option>

                <option value="05">May</option>
                <option value="06">Jun</option>
                <option value="07">Jul</option>
                <option value="08">Aug</option>
                <option value="09">Sep</option>
                <option value="10">Oct</option>

                <option value="11">Nov</option>
                <option value="12">Dec</option>
              </select>
              <select id="review-date-day" name="day">
                <option value="01">1</option>
                <option value="02">2</option>
                <option value="03">3</option>
                <option value="04">4</option>
                <option value="05">5</option>
                <option value="06">6</option>
                <option value="07">7</option>
                <option value="08">8</option>
                <option value="09">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
                <option value="31">31</option>
              </select>
              <select id="review-date-year" name="year">
                <option value="2004">2004</option>
                <option value="2005">2005</option>
                <option value="2006">2006</option>
                <option value="2007">2007</option>
                <option value="2008">2008</option>
                <option value="2009">2008</option>
                <option value="2010">2008</option>
                <option value="2011">2008</option>
                <option value="2012">2008</option>
              </select>
            </div>

      <input type="submit" value="submit" >
    </form>


    <?php 

    $myFile 
    "test.php";
    $fh fopen($myFile'a') or die("can't open file");
    $stringData "
    <div class='tstyle'><img src='/images/testimonial-images/comingSoon.png' width='263' height='210' align='left' />
    <div id='hreview' class='hreview'>
    <h2 class='summary'>$Title</h2>
    <abbr class='dtreviewed bold'>$mnth-$day-$year</abbr> by <span class='reviewer vcard'><span class='fn cgreen'>$Rname</span></span>
    <span style='display: none;' class='type'>business</span><div class='item vcard hide'><span class='fn org'>Organization Here</span><div class='tel'>(###) ###-####</div><div class='adr'><div class='street-address'>Address Here</div><span class='locality'>City</span><span class='region'> ST</span>, <span class='postal-code'> ZIP</span></div></div>
    <blockquote class='description'><p><abbr class='rating hide' title='$Rate'> $Rate out of 5 Stars</abbr>$Sumr</p></blockquote></div>
    <p>&nbsp;</p><div class='fb-like' data-href='http://URL' data-send='false' data-width='335' data-show-faces='false' data-action='recommend'></div> </div>

    <br />



    "
    ;
    fwrite($fh$stringData);
    fclose($fh);


    ?>
    Last edited by ARCLite Studio; 01-01-2012 at 02:17 AM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Of course it will do it twice - once when you call the script (and there is no input) and then once when you submit it:

    Try this:
    PHP Code:
    <?php
    $Rname 
    $_POST["rname"];
    $Rate $_POST["rating"];
    $Title $_POST["title"];
    $Sumr $_POST["summary"];
    $mnth $_POST["month"];
    $day $_POST["day"];
    $year $_POST["year"];
    ?>

    <form id="form1" method="post" action="<?php echo $PHP_SELF;?>">
      Reviewers Name:<input name="rname" type="text" size="30" maxlength="30">
      <br>
      <div xmlns="http://www.w3.org/1999/xhtml" class="field">
              <label for="rating">rating</label>
              <select id="rating" name="rating">
                <option/>
                <option label="★" value="1">1</option>
                <option label="★★" value="2">2</option>
                <option label="★★★" value="3">3</option>

                <option label="★★★★" value="4">4</option>
                <option label="★★★★★" value="5">5</option>
              </select>
              <label id="summary-label" for="title">Title</label>
              <input type="text" id="title" name="title"/><br>
              <label id="summary-label" for="title">Review</label>
              <textarea rows="10" cols="30" name="summary" id="summary"></textarea><br>
              <label for="review-date-month">review date</label>
              <select id="review-date-month" name="month">
                <option value="01">Jan</option>
                <option value="02">Feb</option>
                <option value="03">Mar</option>
                <option value="04">Apr</option>

                <option value="05">May</option>
                <option value="06">Jun</option>
                <option value="07">Jul</option>
                <option value="08">Aug</option>
                <option value="09">Sep</option>
                <option value="10">Oct</option>

                <option value="11">Nov</option>
                <option value="12">Dec</option>
              </select>
              <select id="review-date-day" name="day">
                <option value="01">1</option>
                <option value="02">2</option>
                <option value="03">3</option>
                <option value="04">4</option>
                <option value="05">5</option>
                <option value="06">6</option>
                <option value="07">7</option>
                <option value="08">8</option>
                <option value="09">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
                <option value="31">31</option>
              </select>
              <select id="review-date-year" name="year">
                <option value="2004">2004</option>
                <option value="2005">2005</option>
                <option value="2006">2006</option>
                <option value="2007">2007</option>
                <option value="2008">2008</option>
                <option value="2009">2008</option>
                <option value="2010">2008</option>
                <option value="2011">2008</option>
                <option value="2012">2008</option>
              </select>
            </div>

      <input type="submit" value="submit" >
    </form>


    <?php 
    if (!empty($_POST))
    {
    $myFile "test.php";
    $fh fopen($myFile'a') or die("can't open file");
    $stringData "
    <div class='tstyle'><img src='/images/testimonial-images/comingSoon.png' width='263' height='210' align='left' />
    <div id='hreview' class='hreview'>
    <h2 class='summary'>$Title</h2>
    <abbr class='dtreviewed bold'>$mnth-$day-$year</abbr> by <span class='reviewer vcard'><span class='fn cgreen'>$Rname</span></span>
    <span style='display: none;' class='type'>business</span><div class='item vcard hide'><span class='fn org'>Organization Here</span><div class='tel'>(###) ###-####</div><div class='adr'><div class='street-address'>Address Here</div><span class='locality'>City</span><span class='region'> ST</span>, <span class='postal-code'> ZIP</span></div></div>
    <blockquote class='description'><p><abbr class='rating hide' title='$Rate'> $Rate out of 5 Stars</abbr>$Sumr</p></blockquote></div>
    <p>&nbsp;</p><div class='fb-like' data-href='http://URL' data-send='false' data-width='335' data-show-faces='false' data-action='recommend'></div> </div>

    <br />



    "
    ;
    fwrite($fh$stringData);
    fclose($fh);
    }
    ?>
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    ARCLite Studio (01-01-2012)

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    73
    Thanks
    5
    Thanked 9 Times in 9 Posts

    Thumbs up

    Quote Originally Posted by tangoforce View Post
    Of course it will do it twice
    <---- Haha, you say "of course" like non PHP programmer me should have known that... lol.


    In any case, thank you, If I'm understanding the change correctly it is essentially saying "if POST not empty then continue else die"


    I could be wrong, but either way it fixed the problem thanks TangoForce

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by ARCLite Studio View Post
    In any case, thank you, If I'm understanding the change correctly it is essentially saying "if POST not empty then continue else die"
    Correct
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Tags for this Thread

    Posting Permissions

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