刷新页面时停止插入值到mysql表

时间:2017-03-29 12:03:01

标签: php html mysql

我已经在php中创建了一个名为cash.php的现金表格来输入付款。当我点击提交按钮值成功存储mysql中的付款表时。但是点击提交按钮并刷新页面后插入前一个条目的重复值。 php代码和html代码在同一个页面。我是php.please的新手帮助me.my代码如下。

    <div class="container">
    <div class="row main">
        <div class="main-login main-center">
            <h2>Cash</h2>
            <br>
            <form class="" method="post" action="cash.php">

                <div class="form-group">
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
                            <input type="text" class="form-control" name="mno" id="mno"
                                   placeholder="Member Number"/>
                        </div>
                    </div>
                </div>
                <div class="form-group">

                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-users fa-lg"
                                                               aria-hidden="true"></i></span>
                            <input type="text" class="form-control" name="mname" id="mname"
                                   placeholder="Member Name"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-newspaper-o fa" aria-hidden="true"></i></span>
                            <input type="text" class="form-control" name="nic" id="nic" placeholder="NIC"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">

                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-money fa-lg"
                                                               aria-hidden="true"></i></span>
                            <input type="text" class="form-control" name="amount" id="amount" placeholder="Amount"/>
                        </div>
                    </div>
                </div>

                <div class="form-group">

                    <div class="cols-sm-10">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-calendar-o fa"
                                                               aria-hidden="true"></i></span>
                            <select class="form-control" name="month" id="month">
                                <option selected disabled>Month</option>
                                <option value="January">January</option>
                                <option value="February">February</option>
                                <option value="March">March</option>

                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="cols-sm-10">
                        <label for="number" class="cols-sm-2 control-label">Pay Date</label>
                        <div class="input-group">
                            <span class="input-group-addon"><span class="fa fa-calendar fa"
                                                                  aria-hidden="true"></span></span>
                            <input class="form-control" name="paydate" id="paydate" type="date">
                        </div>
                    </div>
                </div>

                <div class="form-group " align="center">
                    <input style="box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);border: 2px solid #4CAF50;background-color: white;color: black;width: 50%;"
                           href="" target="_blank" type="submit" id="buttonpayment" name="buttonpayment1"
                           class="btn btn-primary btn-lg btn-block login-button" value="Submit"></input>
                </div>

            </form>
        </div>
    </div>
</div>

</body>

<?php

$conn = mysqli_connect("localhost", "root", "", "doctor");

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['buttonpayment1'])) {

    $number = $_POST["mno"];
    $name = $_POST["mname"];
    $nic = $_POST["nic"];
    $amount = $_POST["amount"];
    $month = $_POST["month"];
    $paydate = $_POST["paydate"];

    if ($number == "" || $name == "" || $nic == "" || $amount == "" || $month == "" || $paydate == "") {
        ?>
        <script>alert("There are some uncompleted fields.complete all fields to do the action")</script>
    <?php
    }
    else {


    $sql = "insert into payments (Mid,Mname,Mnic,amount,month,paydate,paymenttype) values('$number','$name','$nic','$amount','$month','$paydate','Cash')";

    if ($conn->query($sql) === TRUE) {

    ?>
        <script>alert("Cash Payment is Successful2")</script>
    <?php


    } else {
    ?>
        <script>alert("Occured Error!!!! TRY AGAIN CASH PAYMENTS")</script>
        <?php
    }
    }
}
$conn->close();
exit;
?>

0 个答案:

没有答案