1

I'm new in PHP. some one please help to stop inserting blank data when refreshing the browser. I'm trying to insert data in SQL database from a HTML form & showing them in the same page in a table. here is my from & PHP code Thanks in advance....

</head>
<body>


        <?php
    $sub = mysql_connect ("localhost", "root","");
    if (!$sub)
    {
        die('Could Not Connect:'.mysql_erro());
    }
    mysql_select_db("test", $sub);
    $sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
    if (!mysql_query($sql, $sub))
    {
        die('Error:'.mysql_error());
    }

    echo 'One Record added';

            $result=mysql_query("SELECT * FROM te");
            echo "<table border='1'>;
            <tr>
            <th>Date</th>
            <th>Sadi</th>
            <th>Jam</th>
            <th>Washi</th>
            </tr>";
            while ($row = mysql_fetch_array($result)) 
            {
            echo "<tr>";
            echo "<td>" .$row['Dat']."</td>";
            echo "<td>" .$row['Sadi']."</td>";
            echo "<td>" .$row['Jam']."</td>";
            echo "<td>" .$row['Washi']."</td>";
            echo "</tr>";
            }
            echo "</table>";
            Mysql_close($sub);
            ?>
    </body>

    <form action="index.php" method="post">
        <input type="text" name="Dat" placeholder="Date, DD-MM-YY">
        <input type="text" name="Sadi" placeholder="Sadi">
        <input type="text" name="Jam" placeholder="Jam">
        <input type="text" name="Washi" placeholder="Washi">
        <input type="submit" name="sub">
    </form>
sadi
  • 61
  • 2
  • 12

1 Answers1

0

Just verify if the $_POST is not empty

if(!empty($_POST))
{
    $sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
    if (!mysql_query($sql, $sub))
    {
        die('Error:'.mysql_error());
    }
}
Taki
  • 17,320
  • 4
  • 26
  • 47