刷新页面时php mysql重复插入

时间:2015-03-16 06:02:21

标签: php mysql

我有表格从用户那里获取一些数据和文件并将其插入到mysql DB

并完成了。但是当我获得消息成功并且文件被上传并且如果我刷新页面相同的数据在DB内重复

当我刷新页面

时,我不希望DB内有重复数据

这是我的代码:

<?php
$Name=mysql_real_escape_string($_POST['nameofresearch']) ?
$_POST['nameofresearch'] : '';
$Dates=mysql_real_escape_string($_POST['dateofstart']) ?
$_POST['dateofstart'] : '';
$Datee=mysql_real_escape_string($_POST['dateofend']) ?
$_POST['dateofend'] : '';
$budget=mysql_real_escape_string($_POST['budget']) ?
$_POST['budget'] : '';
$Names=mysql_real_escape_string($_POST['requestedby']) ?
$_POST['requestedby'] : '';
$executingagency=mysql_real_escape_string($_POST['executingagency']) ?
$_POST['executingagency'] : '';
$projectstatus =mysql_real_escape_string($_POST['projectstatus']) ?
$_POST['projectstatus'] : '';
$projectmanager =mysql_real_escape_string($_POST['projectmanager']) ?
$_POST['projectmanager'] : '';
$briefdes =mysql_real_escape_string($_POST['briefdes']) ?
$_POST['briefdes'] : '';
if($_POST['expert'][0]) { $first_expert   = $_POST['expert'][0]; } else { $first_expert  = 0; }
if($_POST['expert'][1]) { $second_expert  = $_POST['expert'][1]; } else { $second_expert  = 0; }
if($_POST['expert'][2]) { $third_expert   = $_POST['expert'][2]; } else { $third_expert  = 0; }

include("../config.php");
if(is_uploaded_file($_FILES['fileupload']['tmp_name'])) { 
$fname = "ERI_db/".date("H-i-s").strstr($_FILES['fileupload']['name'],'.'); 
} else { echo "<BR>No file uploaded<BR>"; }

$sql="INSERT INTO " . ERI_TABLE . "    (id,nameofresearch,dateofstart,dateofend,budget,requestedby,executingagency, projectstatus,projectmanager,briefdes,filename,expert1,expert2,expert3) 
               VALUES(NULL, '$Name', '$Dates', '$Datee', '$budget', '$Names','$executingagency', '$projectstatus', '$projectmanager', '$briefdes','$fname','$first_expert','$second_expert','$third_expert')";
$result=mysql_query($sql); 
if($result)
{
    echo "<BR><b>Successful<BR>";
    echo "<BR>";
    //----------------------------------------
    if($_FILES['fileupload']['size']>0)
    {
        if(!in_array($_FILES['fileupload']['type'],$exten)){ $r.="امتداد الملف خطا -    <br>";} 
        if(file_exists($dir.$_FILES['fileupload']['name'])){ $r.="الملف موجودة مسبقا -<br>";}
        if($_FILES['fileupload']['size']>$bigsize){ $r.="حجم الملف اكبر من المسموح به -<br>";}
    }
    if($r)
    {
        echo $r;
        exit();
    }
    else
    {
        if($_FILES['fileupload']['tmp_name'])
        {
            copy($_FILES['fileupload']['tmp_name'],$dir.$fname);
            echo "<BR>File is uploaded<BR>";
            // echo "<BR>" . $dir.$fname . "<BR>"; // Print path to file 
            echo "<BR>";
        }}
} else {
echo "<BR>ERROR<BR>";
echo mysql_error();
}

我该怎么办? 请描述解决方案

5 个答案:

答案 0 :(得分:1)

  

并完成了。但是当我获得消息成功并且文件被上传并且如果我刷新页面相同的数据在DB内重复

您应该在使用成功错误消息上传文件后将页面重定向到其他位置,以防止出现此问题。

答案 1 :(得分:0)

HI成功执行查询后,只需将控件重定向到其他页面或同一页面

答案 2 :(得分:0)

由于您使用的是POST方法,因此您应该将保存过程页面重定向到新页面,以显示成功或失败。

编辑:

$result=mysql_query($sql);

之后添加类似的内容
if($result){
    //If success code
    header('Location: page.php?msg=success');
}
else {
    //If error code
    header('Location: page.php?msg=error');
}

在page.php中,可以是这样的......

if(isset($_GET['msg'])){
    $msg=$_GET['msg'];
    if($msg=="success"){
        //Your success message
    }
    else if($msg=="error"){
        //Your error message
    }
}

答案 3 :(得分:0)

@ nasr25,它应该是简单的PHP重定向脚本

header("Location: Your redirection page path"); /* Redirect browser */

出口();

Ref.Link: http://php.net/manual/en/function.header.php

答案 4 :(得分:-1)

<?php
if(isset($_POST)){
$Name=mysql_real_escape_string($_POST['nameofresearch']) ?
$_POST['nameofresearch'] : '';
$Dates=mysql_real_escape_string($_POST['dateofstart']) ?
$_POST['dateofstart'] : '';
$Datee=mysql_real_escape_string($_POST['dateofend']) ?
$_POST['dateofend'] : '';
$budget=mysql_real_escape_string($_POST['budget']) ?
$_POST['budget'] : '';
$Names=mysql_real_escape_string($_POST['requestedby']) ?
$_POST['requestedby'] : '';
$executingagency=mysql_real_escape_string($_POST['executingagency']) ?
$_POST['executingagency'] : '';
$projectstatus =mysql_real_escape_string($_POST['projectstatus']) ?
$_POST['projectstatus'] : '';
$projectmanager =mysql_real_escape_string($_POST['projectmanager']) ?
$_POST['projectmanager'] : '';
$briefdes =mysql_real_escape_string($_POST['briefdes']) ?
$_POST['briefdes'] : '';
if($_POST['expert'][0]) { $first_expert   = $_POST['expert'][0]; } else { $first_expert  = 0; }
if($_POST['expert'][1]) { $second_expert  = $_POST['expert'][1]; } else { $second_expert  = 0; }
if($_POST['expert'][2]) { $third_expert   = $_POST['expert'][2]; } else { $third_expert  = 0; }

include("../config.php");
if(is_uploaded_file($_FILES['fileupload']['tmp_name'])) { 
$fname = "ERI_db/".date("H-i-s").strstr($_FILES['fileupload']['name'],'.'); 
} else { echo "<BR>No file uploaded<BR>"; }

$sql="INSERT INTO " . ERI_TABLE . "    (id,nameofresearch,dateofstart,dateofend,budget,requestedby,executingagency, projectstatus,projectmanager,briefdes,filename,expert1,expert2,expert3) 
               VALUES(NULL, '$Name', '$Dates', '$Datee', '$budget', '$Names','$executingagency', '$projectstatus', '$projectmanager', '$briefdes','$fname','$first_expert','$second_expert','$third_expert')";
$result=mysql_query($sql);
if($result)
{
    echo "<BR><b>Successful<BR>";
    echo "<BR>";
    //----------------------------------------
    if($_FILES['fileupload']['size']>0)
    {
        if(!in_array($_FILES['fileupload']['type'],$exten)){ $r.="امتداد الملف خطا -    <br>";} 
        if(file_exists($dir.$_FILES['fileupload']['name'])){ $r.="الملف موجودة مسبقا -<br>";}
        if($_FILES['fileupload']['size']>$bigsize){ $r.="حجم الملف اكبر من المسموح به -<br>";}
    }
    if($r)
    {
        echo $r;
        exit();
    }
    else
    {
        if($_FILES['fileupload']['tmp_name'])
        {
            copy($_FILES['fileupload']['tmp_name'],$dir.$fname);
            echo "<BR>File is uploaded<BR>";
            // echo "<BR>" . $dir.$fname . "<BR>"; // Print path to file 
            echo "<BR>";
        }}
} else {
echo "<BR>ERROR<BR>";
echo mysql_error();
}
unset($_POST);
}else{
// redirect to your page.
}