简体   繁体   English

数据自动插入数据库中的PHP

[英]data insert into database automatically in php

I have a problem which is the user when write in my comments form is insert successfully but when I refresh the page it will insert the last comments again , I read the solution in this link how to stop data automatically insert into database in php 我有一个问题,就是用户在我的评论表单中写入成功时会插入用户,但是当我刷新页面时它将再次插入最后一个评论,我在此链接中阅读了解决方案, 如何停止将数据自动插入php中的数据库

but does not work for me this is my codes I would appreciate for your help :) 但对我不起作用,这是我的代码,感谢您的帮助

file viewhospital.php contain include comments.php file --look at the bottom of the codes-- viewhospital.php文件包含include comments.php文件-请看代码底部-

 <?php include ('header.php'); if(!isset($_GET['hospital_id'])){ echo '<div class="alert alert-danger" role="alert"><b>You should choose hospital before opening this page!</b></div>'; include ('footer.php'); die(); } include ('setting.php'); $sql = 'select * from hospital where hid = '. $_GET['hospital_id']; $result = $conn->query($sql) or die(mysql_error($conn)); $hospital = null; if ($result->num_rows > 0) { $hospital = $result->fetch_assoc(); } else { die('Could not find hospital!'); } $sql = 'select * from doctor where hospital_id = '. $_GET['hospital_id']; $doctor_result = $conn->query($sql) or die(mysql_error($conn)); $conn->close(); ?> <div class="row"> <div class="col-md-6"> <p class="text-center"> <img src="<?php echo $hospital['image']; ?>" class="img-thumbnail" style="height: 400px;"> </p> </div> <div class="col-md-6"> <p class="text-center"> <img class="img-thumbnail" src="https://maps.googleapis.com/maps/api/staticmap?center=<?php echo $hospital['location']; ?>&zoom=13&size=400x400&maptype=roadmap&markers=color:blue%7Clabel:S%7C<?php echo $hospital['location']; ?>&key=AIzaSyD59nHXpZgqZwjJvsAcPe2CYcIEWoaQ9yY" style="height: 400px;"> </p> </div> </div> <div class="row"> <div class="col-md-12"> <h1 class="page-header"> <?php echo $hospital['name']; ?> </h1> <p> <?php echo $hospital['description']; ?> </p> <p> Address: <?php echo $hospital['address']; ?> </p> <p> Phone: <?php echo $hospital['phone']; ?> </p> <p> <a href="<?php echo $hospital['link1']; ?>">Go To Hospital</a> </p> <p> <a href="<?php echo $hospital['link2']; ?>">Online Appointment</a> </p> </div> </div> <!--<div class="row"> <div class="col-md-12 text-center"> <div class="btn-group" role="group" aria-label="..."> <a type="button" class="btn btn-info">Edit</a> <a type="button" class="btn btn-danger">Remove</a> <a type="button" class="btn btn-primary" href="doctor_form.php?hospital_id=<?php echo $hospital['hid']; ?>">Add Doctor</a> </div> </div> </div>--> <div class="row"> <div class="col-md-12"> <table class="table table-striped"> <caption>Doctors:</caption> <thead> <tr> <th>#</th> <th>Name</th> <th>Field</th> <th></th> </tr> </thead> <tbody> <?php if ($doctor_result->num_rows > 0) { while($row = $doctor_result->fetch_assoc()) { ?> <tr> <th scope="row"> <?php echo $row['did'];?> </th> <td> <?php echo $row['name'];?> </td> <td> <?php echo $row['field'];?> </td> <td><a href="view_hospital.php?doctor_id=<?php echo $row['did']; ?>" class="btn btn-success pull-right">View</a></td> </tr> <?php } }else{ ?> <tr> <th scope="row"></th> <td>No doctors found</td> <td></td> </tr> <?php } ?> </tbody> </table> </div> </div> <?php include ('comments.php'); include ('footer.php'); ?> 

the comments.php file comments.php文件

 <?PHP # comments PHP code date_default_timezone_set('Asia/Riyadh'); function setComments (){ if (isset($_POST['submitComments'])){ include('setting.php'); //$uid = $_POST['uid']; $date = $_POST['date']; $message = $_POST['message']; $sql = "INSERT INTO comments ( date, message) VALUE ( '$date', '$message')"; $result = mysqli_query($conn,$sql); } } function getComments (){ if (isset($_POST['submitComments'])){ include('setting.php'); $sql = "SELECT * FROM comments"; $result = mysqli_query($conn,$sql); while ($row = $result->fetch_assoc()){ echo "<div class='comments-box'>"; echo $row['date']."<br>"; echo nl2br($row['message'])."<br><br>"; echo "</div>"; } } } echo " <form action='".setComments ()."' method='POST'> <input type='hidden' name='uid' value=''> <input type='hidden' name='date' value='".date('Ymd H:i:s')."'> <textarea name='message' class='form-control' rows='3'></textarea> <br> <button type='submit' name='submitComments' class='btn btn-primary'>Comments</button> </form> <br><br> "; getComments (); ?> 

When you refresh in the browser, you send the last request again. 在浏览器中刷新时,您会再次发送上一个请求。 That request was the POST of the form. 该请求是表单的POST。 So the user (browser) is telling the code to insert another comment. 因此,用户(浏览器)正在告诉代码插入另一个注释。

Generally this is handled by redirecting after posting a form, rather than re-displaying the form again. 通常,这是通过发布表单后重定向而不是重新显示表单来处理的。 Move all of your logic for (and only for) inserting the new content to its own PHP file (something like addComment.php ) and have the form post to that file. 移动所有逻辑(仅用于),将新内容插入其自己的PHP文件(类似于addComment.php ),并将表单发布到文件。 Then in that file ensure that there is no actual output except perhaps to display an error message if something goes wrong?) and just a redirect back to the page: 然后在该文件中确保没有实际输出,除非出现问题时可能显示一条错误消息?),然后重定向回页面:

header("Location: viewhospital.php");

This will instruct the browser in the response to make a new GET request for viewhospital.php . 这将指示浏览器在响应中对viewhospital.php进行新的GET请求。 So if the user reloads the browser, all they're doing is repeating that GET request. 因此,如果用户重新加载浏览器,那么他们要做的就是重复该GET请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM