2014-03-05 162 views
-1

我有一個代碼將數據插入到數據庫中,並且在數據成功插入到數據庫後,用戶需要刷新頁面以查看最近插入的數據的標題。所以我的問題是,當我刷新頁面的數據有重複,請你糾正我的代碼:停止重新提交頁面時刷新數據,php

<!DOCTYPE html5> 
<html> 
    <head><title>Admin Page Penuel Construction</title> <link rel='stylesheet' type='text/css' href='AdminCss.css'/> 

     <meta name='description' content='Laboratory Activity 10'> 
     <meta name='keywords' content='HTML,CSS,JavaScript,PHP'> 
     <meta name='author' content='Monterde, Krystine Ann Marie'> 
     <meta charset='UTF-8'> 
     <script src="validation.js" type="text/javascript" ></script> 
    </head> 
    <body id='main_background'> 
    <center> 
     <div id='container'> 
      <div id='header'> </div> 
      <div id='mainContent'> 
       <div id='leftContent'> 
       <div id='greeting_box'><p id='greeting_style'>HELLO ADMIN!</p></div> 
       <div id='sidenav_title'><div class='sidenav_style_title color_nav_background_one' >Penuel Construction</div> 
       <table class='table_division_nav'> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td><a href='BuildingBlocks_Penuel_News_Unpublished.html' class='style_text_nav' >News/Updates</a></td></tr> 
       <tr><td><a href='BuildingBlocks_Penuel_Projects_Ongoing.html' class='style_text_nav' >Projects</a></td></tr> 
       <tr><td><a href='BuildingBlocks_Penuel_About_Us.html' class='style_text_nav' >About Us</a></td></tr> 
       </table></div> 
       <div id='sidenav_title_two'><div class='sidenav_style_title color_nav_background_two'>Samantha's Place</div> 
       <table class='table_division_nav'> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td><a href='BuildingBlocks_Samantha_News_Unpublished.html' class='style_text_nav' >News/Updates</a></td></tr> 
       <tr><td><a href='BuildingBlocks_Samantha1.html' class='style_text_nav' >Projects</a></td></tr> 
       <tr><td><a href='BuildingBlocks_Samantha_Promos_Unpublished.html' class='style_text_nav' >Promos</a></td></tr> 
       </table></div> 
       <div id='sidenav_title'><div class='sidenav_style_title color_nav_background_three'>Others</div> 
       <table class='table_division_nav'> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td></td></tr> 
       <tr><td><a href='BuildingBlocks_Others.html' class='style_text_nav' >Contact Us</a></td></tr> 
       <tr><td><a href='#' class='style_text_nav' >Log Out</a></td></tr> 

       </table></div> 
       </div> 
       <?php 
       //getting data from database 
        include('connect_to_db.php'); 
        $querys="SELECT * FROM pc_news"; 
        $result=mysql_query($querys); 
        $num=mysql_numrows($result); 
        mysql_close($link); 
       ?> 
       <div class='color_nav_background_one' id='rightcontent'> 
        <div id='right_content_upper'> 
        <div class='projects_navigation_news'><a href='BuildingBlocks_Penuel_News_Unpublished.html' >Unpublished</a></div><div class='projects_navigation_news'> <a href='BuildingBlocks_Penuel_News_Published.html'>Published</a></div> 
         <div id='divsion_under_news_navigation_Unpublished'></div> 

          <table id='table_sytle'> 
           <tr><td></td></tr> 
           <tr><td></td></tr> 
           <tr><td></td></tr> 
           <?php 

            //viewing data from database 
            $i=0; 
            while ($i < $num) 
            { 

             $title=mysql_result($result,$i,"title"); 
           ?> 
           <tr> 

           <?php print"<td><p class='rightcontent_upper_text_sytle'> $title </p></td><td></td><td onclick><a href='####' class='action_textstyle_table'id='color_action_edit'>Edit</a></td><td onclick><a href='#' class='action_textstyle_table' id='color_action_delete'>Delete</a></td><td onclick><a href='#' class='action_textstyle_table' id='color_action_complete_or_published'>Published</a></td></tr> "; ?> 

           </tr> 

           <?php $i++;} ?> 
          </table> 

         <div class='postion_paging'>1-5/150</div> 
         <div id='postion_select'><input type='button' value='<<Prev' class='button_style'><input type='button' value='Next>>' class='button_style'></div>       


        </div> 

        <div id='right_content_lower_left'> 
        <form name = "unpublished_check" onsubmit = "return unpublishedCheck()" action="sampleNewscode.php" method ="post" enctype = "multipart/form-data"> 
         <table > 
         <tr><td> Choose Status: </td> 
          <td><input name="status" type='radio' value='0' />Unpublished<input name="status" type='radio' value='1'/>Published</td></tr> 
         <tr><td><p class='rightcontent_lower_left_text_sytle'>Title:</p></td><td><input type='text' id='textbox_style' name = 'title'/></td></tr> 
         <tr><td> Attach Image/s:</td> 
          <td><input type='file' name='photo' accept="image/*" /> <input type = 'hidden' name = 'MAX_FILE_SIZE' value = '5000000'></td></tr> 
         <tr><td><p class='rightcontent_lower_left_text_sytle'>Content:</p></td> 
          <td><textarea rows='7' cols='70' name = 'txtarea'></textarea></td></tr> 
         <tr><td><input type='submit' name='submit' value='Submit'/></td></tr> 
         </table> 
        </form> 
        </div> 
       </div> 
      </div> 
      <div id='footer'></div> 
     </div> 
    </center> 
    <?php 
     //inserting data unto database 
     if(isset($_POST['submit'])) 
     { 
      include('connect_to_db.php'); 
      $status = $_POST['status']; 
      $dir = "images/"; 
      $tmp_name = $_FILES['photo']['tmp_name']; 
      $pic = $_FILES['photo']['name']; 
      $title = mysql_real_escape_string ($_POST['title']); 
      $txtarea = mysql_real_escape_string ($_POST['txtarea']); 


      if(move_uploaded_file($tmp_name, $dir . $pic)){ 
      $query = "INSERT INTO pc_news VALUES('$title','$txtarea','$pic','$status')"; 
      mysql_query($query) or die(mysql_error()); 

      } 
      else{ 
       echo "Sorry, there was a problem uploading your file."; 
      } 
      mysql_close($link); 

     }  
    ?> 

    </body> 
</html> 
+0

有一個在一個問題與您的名字完全相同的權利----------------------------------------- ----------------> –

+1

爲什麼不偶爾搜索一個答案,而不是詢問?只是爲了改變? –

+0

可能重複[如何停止頁面重新刷新頁面?](http://stackoverflow.com/questions/15081323/how-to-stop-resubmitting-of-page-on-refreshing-the-page) –

回答

0

header('location:your_current_page.php'); 

mysql_close($link);函數調用之後。

+1

我有兩個mysql_close ..我在哪裏把它..在兩個? – ChocoLover

+0

後最後一個..你通過你的插入查詢.. –

+0

謝謝...它解決了我的問題! – ChocoLover

0

之後mysql_close($link);重定向到同一頁面,否則當您按F5時,表單將被重新提交。

-1

使用標題並重定向頁面。

header("Location:your_page.php"); 

您可以重定向到相同的頁面或不同的頁面。

在將數據插入到數據庫後取消設置$ _POST。

unset($_POST); 
+0

這個未設置($ _ POST)有什麼意義;? –

+0

@YourCommonSense是啊..我在想同一個.. –

0

永久的解決方案...

閱讀Post/Redirect/Get

你必須從當前頁面重定向你的網頁到另一頁後提交的代碼執行...