数据在php

时间:2017-04-04 16:30:00

标签: php

我有一个问题,就是用户在我的评论表单中写入时成功插入但当我刷新页面时会再次插入最后一条评论,我在此链接中阅读了解决方案how to stop data automatically insert into database in php

但对我不起作用 这是我的代码,我将非常感谢您的帮助:)

文件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');
?>

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('Y-m-d 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 ();
 ?>
  

1 个答案:

答案 0 :(得分:1)

在浏览器中刷新时,会再次发送最后一个请求。该请求是表单的POST。因此,用户(浏览器)告诉代码插入另一条评论。

通常,这是在发布表单后由重定向处理,而不是再次重新显示表单。移动所有逻辑(并且仅用于)将新内容插入到自己的PHP文件(类似addComment.php)并将表单发布到 文件。然后在该文件中确保没有实际输出,除非出现错误时显示错误消息?)并且只是重定向回页面:

header("Location: viewhospital.php");

这将指示响应中的浏览器为viewhospital.php发出新的GET请求。因此,如果用户重新加载浏览器,他们所做的只是重复该GET请求。