当我重新加载我的页面时,从$ _POST插入数据

时间:2017-12-30 07:57:03

标签: javascript php html

当我再次重新加载页面时,从$_POST插入数据,我无法删除$_POST数据和浏览器Cookie。

if($_POST['submit']) {
    $title         = $_POST['title']; 
    $location      = $_POST['location']; 
    $description   = $_POST['description'];

    $sql = "INSERT INTO `room_list` (`id`, room`) VALUES (NULL, '0');";
    $query = mysql_query($sql);

    if($query){    // True  }
    else {   // False  } 

    unset($_POST[submit]); 
    unset($_REQUEST);
}

1 个答案:

答案 0 :(得分:0)

首先,您应该在尝试将变量分配为变量之前测试POST变量是否可用,将代码升级为使用mysqliPDO并避免重新提交表单,这是常用的header函数在某些特定操作/表单提交后重定向以避免多次插入

if( isset( $_POST['submit'], $_POST['title'],$_POST['location'],$_POST['description'] ) ){

    /* why do these not get used? */
    $title         = $_POST['title']; 
    $location      = $_POST['location']; 
    $description   = $_POST['description'];

    /* Should use `mysqli` or `PDO` - the `mysql` api is deprecated */
    $sql = "INSERT INTO `room_list` (`id`, `room`) VALUES (NULL, '0');";
    $query = mysql_query( $sql );


    header( "Location: " . $_SERVER['SCRIPT_NAME'] . '?status='.$query );

}