2017-10-04 32 views
0

我使用两个脚本,一个用于从SQL中删除,另一个用于添加到数据库。使用标题时,从一个脚本刷新数据,而不是从另一个脚本?

我遇到的问题是,在运行添加脚本时,它将刷新即时显示更改的页面。然而,在运行删除脚本时,它不会立即刷新它,似乎它会缓存输出?

如果它确实缓存了它重定向到的输出,为什么一个脚本显示添加而另一个不显示删除?

添加脚本

// Set a URL for the user to be redirected to 
$header_URL = "Location: ".WEBURL.DOCROOT."pages/parents-evening/{$_SESSION['status']}/"; 


// SQL statement using the variables from the user to insert into a specific table 
$sql = "INSERT INTO $table ($columns) VALUES ($values);"; 

// Check that the query was successful 
if(mysqli_query($conn, $sql)) 
{ 
    // Success 
    // Closes the database connection 
    mysqli_close($conn); 
    // Sets the redirect location 
    header($header_URL); 
    // Exits the script 
    exit(); 
} 
else 
{ 
    // Fail 
    // Closes the database connection 
    mysqli_close($conn); 
    // Sets the redirect location 
    header($header_URL); 
    // Exits the script 
    exit(); 
} 

删除脚本

// Set a URL for the user to be redirected to 
$header_URL = "Location: ".WEBURL.DOCROOT."pages/parents-evening/{$_SESSION['status']}/"; 

// SQL statement to delete from the table provided where the ID is equal to either the POST or GET value 
$sql = "DELETE FROM {$table} WHERE id = {$_POST['delete_id']}{$_GET['delete_id']}"; 

// Check the query was successful 
if(mysqli_query($conn, $sql)) 
{ 
    // Success 
    // Closes the database connection 
    mysqli_close($conn); 
    // Sets the redirect location 
    header($header_URL); 
    // Exits the script 
    exit(); 
} 
else 
{ 
    // Fail 
    // Closes the database connection 
    mysqli_close($conn); 
    // Sets the redirect location 
    header($header_URL); 
    // Exits the script 
    exit(); 
} 
+0

“当我删除它不会” 这并不_what_?你继续说它最终会刷新,所以它没有做什么? –

+0

@PatrickQ我已经重写了我的问题。但是,当一个脚本运行时它会立即刷新,另一个脚本不会立即执行。 –

+0

为什么你有'{$ _POST ['delete_id']} {$ _ GET ['delete_id']}''有特定的原因吗?我只希望其中的一个。你真的应该看看参数绑定。 –

回答

0

似乎问题是,在此声明:

$ SQL =“DELETE FROM {$表} WHERE ID = {$ _POST [ 'delete_id']} {$ _ GET [ 'delete_id']}“;

如果从POST越来越delete_id查询将是:

$sql = "DELETE FROM {$table} WHERE id = ".$_POST['delete_id']; 

如果从GET越来越delete_id查询将是:

$sql = "DELETE FROM {$table} WHERE id = ".$_GET['delete_id']; 

如果只有一个记录是删除(因为似乎id是主键,所以只会有一条记录),我会推荐使用limit 1,因为它会加快删除过程,如果你有巨大的da ta在你的桌子上。

限制1将告诉数据库只删除一条记录,因此当1条记录被删除时它将停止执行。

你最终的查询将是:

对于POST

$sql = "DELETE FROM {$table} WHERE id = ".$_POST['delete_id']." LIMIT 1"; 

对于GET

$sql = "DELETE FROM {$table} WHERE id = ".$_GET['delete_id']." LIMIT 1"; 
+0

我的问题不在于如上所述执行的代码。 SQL语句在当前状态下成功运行。问题是,插入数据时,它重定向的页面会立即显示更改。删除数据时,它指向的页面不会立即显示更改,并且比我预期在页面上显示的时间要多得多。这显示为在DBMS自身内部立即完成。 –

相关问题