为什么我的SQL在刷新页面时插入相同的数据?

为什么我的SQL在刷新页面时插入相同的数据?

问题描述:

I'm new in php coding. I'm creating a comment section but when i submit comment every time same comment inserting into my database. here is my php code.

     <?php 
     function setComments( $mysqli ){
     if (isset($_POST['commentSubmit'])) {
       $uid= $_POST['uid'];
       $date= $_POST['date'];
       $message= $_POST['message'];

      $sql="INSERT INTO comments(uid,date,message) VALUES('$uid','$date','$message')";

    $result = $mysqli-> query($sql);
  }
}
function getComments( $mysqli ){
  $sql = "SELECT * FROM comments ORDER BY date DESC ";
  $result = $mysqli->query($sql);
     while ( $row = $result->fetch_assoc() ) {
        echo "<div class='comment-box' ><p>";
        echo $row['uid']."<br><br>";
        echo $row['date']."<br><br>";
        echo nl2br($row['message']);
        echo "<p></div>";
  }
}

and here is code for output and submit

<?php

 echo "
   <div align='center'>
      <form method='POST' action='".setComments( $mysqli)."'>
        <input type='hidden' name='uid' value='Anwer'>
        <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
        <textarea name='message'></textarea>
         <br>
        <button type='submit' name='commentSubmit' > Comment</button>
    </form>
    </div>"
    ;
  getComments($mysqli);

  ?>

我是php编码的新手。 我正在创建一个评论部分,但是当我每次在我的数据库中插入相同的注释时都提交注释。 我的PHP代码。 p>

 &lt;?php 
 function setComments($ mysqli){
 if(isset($ _ POST ['commentSubmit'])){
 $ uid = $ _POST  ['uid']; 
 $ date = $ _POST ['date']; 
 $ message = $ _POST ['message']; 
 
 $ sql =“INSERT INTO comments(uid,date,message)  VALUES('$ uid','$ date','$ message')“; 
 
 $ result = $ mysqli-&gt;  query($ sql); 
} 
} 
function getComments($ mysqli){
 $ sql =“SELECT * FROM comments ORDER BY date DESC”; 
 $ result = $ mysqli-&gt; query($ sql  ); 
 while($ row = $ result-&gt; fetch_assoc()){
 echo“&lt; div class ='comment-box'&gt;&lt; p&gt;”; 
 echo $ row ['uid'  ]“&lt; br&gt;&lt; br&gt;”; 
 echo $ row ['date']。“&lt; br&gt;&lt; br&gt;”; 
 echo nl2br($ row ['message']); \  n echo“&lt; p&gt;&lt; / div&gt;”; 
} 
} 
  code>  pre> 
 
 

这里是输出和提交的代码 p> \ n

 &lt;?php 
 
 echo“
&lt; div align ='center'&gt; 
&lt; form method ='POST'action ='”。setComments($ mysqli  )。''&gt; 
&lt;输入类型='隐藏'名称='uid'值='Anwer'&gt; 
&lt;输入类型='隐藏'名称='日期'值='“。date(  'Ymd H:i:s')。“'&gt; 
&lt; textarea name ='message'&gt;&lt; / textarea&gt; 
&lt; br&gt; 
&lt; button type ='submit'name ='  commentSubmit'&gt;评论&lt; / button&gt; 
&lt; / form&gt; 
&lt; / div&gt;“
; 
 getC  omments($ mysqli); 
 
?&gt; 
  code>  pre> 
  div>

Here is the problem. You are calling the save function in runtime without condition. As an example;

<?php 
function setComments( $mysqli, $postArr ){
   $uid=  $postArr['uid'];
   $date= $postArr['date'];
   $message= $postArr['message'];
   $sql="INSERT INTO comments(uid,date,message) 
            VALUES('$uid','$date','$message')";
   $result = $mysqli-> query($sql);
}

// I'm calling save function only for submit event

if (!empty($_POST['commentSubmit'])) {
   setComments( $mysqli, $_POST);
}

 // For your HTML code;

echo "<div align='center'>
        <form method='POST' action=''>
          <input type='hidden' name='uid' value='Anwer'>
          <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
          <textarea name='message'></textarea>
          <br>
          <button type='submit' name='commentSubmit'> Comment</button>
        </form>
    </div>";

getComments($mysqli);
?>