Extra rows being put into database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jessDMiller
    New Member
    • Dec 2006
    • 6

    #1

    Extra rows being put into database

    When I refresh the page or every time I go to it, a blank row is inserted into the database. This can not happen as I'm then printing out all the contents of the database. How can I stop it?

    index.php:

    [PHP]
    <?php include "header.php "; ?>
    <td valign="top"><b r />
    <a href="addAnnoun ce.php">Click Here</a> to enter an announcement. <br /><br />
    <img src="images/announceheader. gif" /><br />
    <?php
    //create variable names
    $heading=$HTTP_ POST_VARS['heading'];
    $announce=$HTTP _POST_VARS['announce'];
    $date=$HTTP_POS T_VARS['date'];

    $heading= trim($heading);
    $announce= trim($announce) ;
    $date= trim($date);

    $heading= addslashes($hea ding);
    $announce= addslashes($ann ounce);
    $date= addslashes($dat e);

    //connect to server
    @ $db = mysql_pconnect( $hostname,$user name,$password) ;

    if (!db)
    {
    echo 'Error: Could not connect to database. Please try again later.';
    exit;
    }

    mysql_select_db ($databaseName) ;

    // insert data into database
    $insert = mysql_query("in sert into $table (heading, announce, date) values ('$heading', '$announce', '$date')")
    or die("Could not insert data because ".mysql_error() );

    ?>
    <?php
    // Get all the data from the "example" table
    $result = mysql_query("SE LECT * FROM $table")
    or die(mysql_error ());

    echo "<table border='0'>";
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_arr ay( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td><f ont class='heading' >";
    echo $row['heading'];
    echo "</font></td></tr><tr><td>";
    echo $row['announce'];
    echo "</td></tr><tr><td><fon t class='date'>";
    echo $row['date'];
    echo "</font></td></tr>";
    echo "<tr><td>&nbsp; </td></tr>";
    }

    echo "</table>";

    mysql_close();
    ?>

    </td>
    </tr>
    </table>
    </body>
    </html>
    [/PHP]

    addAnnounce.php :

    [PHP]<?php
    include "header.php ";
    ?>

    <td align=top>
    <form action="index.p hp" method="post">H eading:<br /><input type="text" size="40" name="heading"/><br /><br />Announcement:< br />
    <textarea name="announce" cols="50" rows="10"></textarea>
    <br /><br />Date:<br /><input type="text" size="10" name="date"/><br /><br /><input type="submit" value="Post" /></form>
    </td>
    </table>
    </body>
    </html>[/PHP]

    Thanks.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    There is nothing to stop it! When loading it just runs all code and thus inserts a row each time.

    What you can do, as the simplest way, is to make the addAnnounce the index.php and the index.php the addAnnounce.php . Then change the action attribute in the form. And check, before the $_POST handling routine if the form is actually submitted. You can do that by including a hidden text field in the form and check the presence of that field before you do any other $_POST handling.

    See this post in the forum for an example http://www.thescripts.com/forum/post2243040-4.html

    Ronald :cool:

    Comment

    • ctsasikumar
      New Member
      • Nov 2006
      • 17

      #3
      Hai check the condition

      [php]

      if($heading!='' )
      {
      //execute insert query......


      after insert clear all values

      $heading='';

      }
      [/php]

      so next time referesh the value is null...
      when will submit the value automatically insert

      Sasi

      Comment

      Working...