-1

I have added a button to my HTML page so that I may truncate my SQL database table whenever the user needs to.

However every time I refresh the page now the table gets truncated, without me pressing the button.

Anyone know the way around this?

Button submit code:

<form method="post" action="eDBase.php">
<input name="truncate_But" type="submit" value=" Truncate Table " />
</form>

Button truncate code:

if(isset($_POST['truncate_But']))
{
    mysqli_query($conn, 'TRUNCATE TABLE `clientinfo`');
    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}
cola465
  • 113
  • 8
  • 1
    If you refresh the page after submitting the form, you'll get a box asking if you want to resubmit the form -- you probably clicked yes. If you load the page, it will not do truncate anything, at least not without clicking the button (with your current code). – Qirel Jan 27 '20 at 09:42
  • Does this answer your question? [How to prevent form resubmission when page is refreshed (F5 / CTRL+R)](https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr) – 04FS Jan 27 '20 at 09:52
  • Not quite sure how to follow the PRG method, is there no way for the truncate method to be sent only once. – cola465 Jan 27 '20 at 18:54

1 Answers1

3
<form method="post" action="eDBase.php">
<input name="truncate_But" type="submit" value="Truncate Table" />
</form>

 if(isset($_POST['truncate_But']) && $_POST['truncate_But'] == "Truncate Table")
 {
    mysqli_query($conn, 'TRUNCATE TABLE `clientinfo`');
    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
 }
Marmik Patel
  • 192
  • 2
  • 12