1

I am trying to create an HTML button, that, when pressed, it will truncate a MySQL table. I used the answer given here. The button works, but the problem is that when I refresh the page, the command is triggered and the table gets truncated, even though I have not pressed the truncate button. How can I fix that? I only have one .php file.

The code so far is:

<?php
$dbc = mysqli_connect('localhost', 'username', '', 'DB_name')
  or die('Error connecting to MySQL server.');
?>

<form method="post" action="<?php mysqli_query($dbc, 'TRUNCATE TABLE `my_table`')   ?>">
    <input name="submit_button" type="submit" value=" Truncate Table " />
</form>

I would not consider my question as a duplicate, because in the other question, there is a redirection to an other .php page.

Community
  • 1
  • 1
  • 1
    action is a url not a php function, the above will never work –  Dec 19 '15 at 05:43
  • the answer in the linked question is 100% what you want –  Dec 19 '15 at 05:44
  • 1
    Also read [this so post](http://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-via-php) – bansi Dec 19 '15 at 05:47
  • The code works, but it works both when I press the button and when refreshing the page. Even when I use the answer in the linked question, the table gets truncated the moment I refresh the page,. Even though I don't press the "truncate button". – danielpanatha Dec 19 '15 at 05:53
  • check the post on my previous comment – bansi Dec 19 '15 at 05:54
  • refresh,can repost, you should redirect to a new page afterwards –  Dec 19 '15 at 05:59
  • but I want to stay on the same page. Isn't that possible? – danielpanatha Dec 19 '15 at 06:04

1 Answers1

0

Use something like this

<?php
$dbc = mysqli_connect('localhost', 'username', '', 'DB_name') or die('Error connecting to MySQL server.'); 
if(isset($_POST['submit_button']))
{
    mysqli_query($dbc, 'TRUNCATE TABLE `my_table`');
    header("Location: " . $_SERVER['PHP_SELF']);
    exit();
}

?>
<form method="post" action="">
    <input name="submit_button" type="submit" value=" Truncate Table " />
</form>
Gaurav Rai
  • 900
  • 10
  • 23
  • If you post reason for a vote down, it would help to improve answer. – Gaurav Rai Dec 19 '15 at 06:02
  • Same result. Refresh empties the table. – danielpanatha Dec 19 '15 at 06:15
  • Updated answer. Now page load will not truncate table, only clicking will truncate. Try it. Let me know if it works, as I guess. – Gaurav Rai Dec 19 '15 at 06:39
  • This does the trick! Thanks a lot!!! I accepted your answer as an answer to my question, but, sadly, it seems like I cannot upvote your answer. Do you know why? – danielpanatha Dec 20 '15 at 17:01
  • Also, I would not consider my question as a duplicate, because in the other question, there is a redirection to an other .php page. But this is up to the admins to decide. – danielpanatha Dec 20 '15 at 17:10
  • Thanks @danielpanatha you're not able to upvote because of website's rules http://stackoverflow.com/help/privileges/vote-up Some Higher Reputed people here think this question as duplicate, and down voted your question and my answer. Stack should make reasons to be mandatory in case of any down vote. Anyways thanks. – Gaurav Rai Dec 21 '15 at 04:49