数据自动插入

时间:2013-04-06 09:07:33

标签: php wordpress

在这段代码中一切正常,它从会话中获取用户名并通过单选按钮完美地插入数据,唯一的问题是当我重新加载页面时它会自动再次插入数据。

if(isset($_SESSION['user_id'])&&isset($_POST['r1'])&&isset($_POST['r2'])&&isset($_POST['r3'])&&isset($_POST['r4'])&&isset($_POST['suggestion'])&&isset($_POST['submit'])){
    $user=$_SESSION['user_id'];
    $r1=$_POST['r1'];
    $r2=$_POST['r2'];
    $r3=$_POST['r3'];
    $r4=$_POST['r4'];
    $suggestion=$_POST['suggestion'];
    $query_insert="INSERT INTO `wp_feedback` VALUES ('".$user."','".$r1."','".$r2."','".$r3."','".$r4."','".$suggestion."')";

    if($query_run=mysql_query($query_insert)){
    echo "alright";
    }
    else {
        echo "not inserted";
        }

}

提前致谢。

4 个答案:

答案 0 :(得分:1)

在脚本底部放置重定向,因为我认为仍然重新加载页面 执行表单提交

if(isset($_SESSION['user_id'])&&isset($_POST['r1'])&&isset($_POST['r2'])&&isset($_POST['r3'])&&isset($_POST['r4'])&&isset($_POST['suggestion'])&&isset($_POST['submit'])){
    $user=$_SESSION['user_id'];
    $r1=$_POST['r1'];
    $r2=$_POST['r2'];
    $r3=$_POST['r3'];
    $r4=$_POST['r4'];
    $suggestion=$_POST['suggestion'];
    $query_insert="INSERT INTO `wp_feedback` VALUES ('".$user."','".$r1."','".$r2."','".$r3."','".$r4."','".$suggestion."')";

    if($query_run=mysql_query($query_insert)){
    echo "alright";
    }
    else {
        echo "not inserted";
        }
header('Location: redirect url');
}

答案 1 :(得分:1)

请试一试,

if(isset($_SESSION['user_id'])&&isset($_POST['r1'])&&isset($_POST['r2'])&&isset($_POST['r3'])&&isset($_POST['r4'])&&isset($_POST['suggestion'])&&isset($_POST['submit']))
{
         $user=$_SESSION['user_id']; 
         $r1=$_POST['r1']; 
         $r2=$_POST['r2']; 
         $r3=$_POST['r3']; 
         $r4=$_POST['r4']; 
         $suggestion=$_POST['suggestion']; 
         $query_insert="INSERT INTO wp_feedback VALUES ('".$user."','".$r1."','".$r2."','".$r3."','".$r4."','".$suggestion."')";


    if(isset($_POST['submit']))
    {

        if($query_run=mysql_query($query_insert))
        {
            echo "alright";
            header('Location:setlocation.php');
        }
        else 
        {
            echo "not inserted";
        }
    }
}

在插入查询后设置标题位置...

答案 2 :(得分:0)

if(isset($_SESSION['user_id'])&&isset($_POST['r1'])&&isset($_POST['r2'])&&isset($_POST['r3'])&&isset($_POST['r4'])&&isset($_POST['suggestion'])&&isset($_POST['submit'])){
    $user=$_SESSION['user_id'];
    $r1=$_POST['r1'];
    $r2=$_POST['r2'];
    $r3=$_POST['r3'];
    $r4=$_POST['r4'];
    $suggestion=$_POST['suggestion'];
    $query_insert="INSERT INTO `wp_feedback` VALUES ('".$user."','".$r1."','".$r2."','".$r3."','".$r4."','".$suggestion."')";

    if($query_run=mysql_query($query_insert)){
    echo "alright";
    header("Location:someurl");//This will redirect into some other pages and solve your problem
    }
    else {
        echo "not inserted";
        }

}

答案 3 :(得分:0)

if(isset($_SESSION['user_id'])&&isset($_POST['r1'])&&isset($_POST['r2'])&&isset($_POST['r3'])&&isset($_POST['r4'])&&isset($_POST['suggestion'])&&isset($_POST['submit'])){
$user=$_SESSION['user_id'];
$r1=$_POST['r1'];
$r2=$_POST['r2'];
$r3=$_POST['r3'];
$r4=$_POST['r4'];
$suggestion=$_POST['suggestion'];
$query_insert="INSERT INTO `wp_feedback` VALUES ('".$user."','".$r1."','".$r2."','".$r3."','".$r4."','".$suggestion."')";

if($query_run=mysql_query($query_insert)){
echo "alright";
}
else {
    echo "not inserted";
    }
 header('Location:yourpage.php');

}