停止在刷新浏览器中插入空白数据

时间:2016-12-23 18:21:56

标签: php sql

我是PHP的新手。请刷新浏览器时,请帮助停止插入空白数据。我正在尝试从HTML表单中插入SQL数据库中的数据。将它们显示在表格的同一页面中。 这是我的来自& PHP代码 提前谢谢....

</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>

1 个答案:

答案 0 :(得分:0)

只需验证$_POST是否为空

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());
    }
}