我有一个将数据插入数据库的代码,在数据成功插入数据库后,用户需要刷新页面以查看最近插入的数据的标题。所以我的问题是当我刷新页面时数据会有重复,请更正我的代码:
<!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 :(得分:0)
把
header('location:your_current_page.php');
在mysql_close($link);
函数调用之后。
答案 1 :(得分:0)
mysql_close($link);
重定向到同一页面后,否则当您按 F5 时,表单将重新提交。
答案 2 :(得分:0)
答案 3 :(得分:-1)
使用标题并重定向页面。
header("Location:your_page.php");
您可以重定向到同一页面或不同页面。
将$ _POST插入数据库后取消设置。
unset($_POST);