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

在页面刷新时重新提交数据,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>

Put

header('location:your_current_page.php');

after your mysql_close($link); function call.

After mysql_close($link); redirect to the same page, otherwise when you press F5, form will get resubmitted.

Use header and redirect the page.

header("Location:your_page.php");

You can redirect to same page or different page.

Unset $_POST after inserting it to Database.

unset($_POST);

for permanent solution ...

read Post/Redirect/Get

You have to redirect your page from current page to another page after submit code executed...