douye2110 2014-03-05 10:08
浏览 87
已采纳

在页面刷新时重新提交数据,php

I have a code that will insert data into database and after the data successfully inserted to the database the user needs to refresh the page to see the title of the data that is recently inserted. So my problem is when I refresh the page the data will have duplicates, please do correct my code:

<!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>
  • 写回答

4条回答 默认 最新

  • douhuai2861 2014-03-05 10:09
    关注

    Put

    header('location:your_current_page.php');
    

    after your mysql_close($link); function call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题