2

So I need simple thing, I need to create button in my website, after button is clicked, It should truncate database table, but I can't do It successfully by myself. So could you help me, please?

Here I'm trying to creeate button:

<input type="button" id='delete' class='delete' value='Truncate' onClick="$truncate">
</input>

I know this is wrong way to use PHP variable in HTML, but I don't know how to do It correctly.

Here is my PHP variable:

$truncate= "TRUNCATE TABLE myTable";

With connection to database is all right:

$mysqli = new mysqli("localhost","database","password","asd");

So maybe here is better method to create button for truncate database's table? Thank you.

UPDATED:

This won't work too, nothing happens after button is clicked.

    if(isset($_POST['delete'])){
    $delete= "TRUNCATE TABLE myTable";
    }
?>
<input type="button" id='delete' class='delete' name="delete" value='Truncate' onClick="delete">
</input>
  • You could use jQuery and ajax to execute the php to truncate the table so on the button click event, you use jQuery ajax to run the php script to truncate the table – AdRock Nov 27 '13 at 12:57
  • Thank you for answer, could you help me with It? –  Nov 27 '13 at 12:58
  • 1
    Easy as Pie. With your input button (and naming it "delete" also), just do this `if(isset($_POST['delete'])){ // code for SQL command to truncate }` simple. – Funk Forty Niner Nov 27 '13 at 13:05
  • Thank you for answer. I updated my question how my code looks like now, but It still won't work. –  Nov 27 '13 at 13:12
  • You're not including any DB credentials, if that's your full code. There's a bit more to it than that. And this line is incorrect `$delete= "TRUNCATE TABLE myTable";` you don't need the `$delete=` but the actual MySQL command to do so. – Funk Forty Niner Nov 27 '13 at 13:13
  • This is not my full code, It before connects to database sucessfully. –  Nov 27 '13 at 13:16
  • Rename your button to this `` and try again then. – Funk Forty Niner Nov 27 '13 at 13:21
  • Renamed, but still the same. How It should look like: `if(isset($_POST['delete'])){ "TRUNCATE TABLE myTable"; }` –  Nov 27 '13 at 13:24
  • Or `$delete = "TRUNCATE TABLE myTable";`? –  Nov 27 '13 at 13:25

2 Answers2

1

Here, give this a try.

PHP (delete_table.php)

<?php
// CONNECT TO THE DATABASE
$DB_HOST = "your_host";
$DB_NAME = "your_DB_name";
$DB_USER = "username";
$DB_PASS = "password";

$dbc = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME)
or die('Error connecting to MySQL server');

if(isset($_POST['delete'])){
$query = "TRUNCATE TABLE `yourTable` "; // replace yourTable with one to delete
$result = mysqli_query($dbc,$query)
or die('Error deleting table.');
}
else {
echo "Sorry";
}
?>

HTML form

<form method="post" action="delete_table.php">
<input type="submit" id='delete' class='delete' name="delete" value='Truncate'></input>
</form>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thank you this worked, possible that after deleting It redirected back from delete_table.php to main.php? Thank you again. –  Nov 27 '13 at 13:32
  • You're welcome. Please mark as accepted. You could add a `header("Location: main.php");` after `or die('Error deleting table.');` – Funk Forty Niner Nov 27 '13 at 13:34
0

I'm by far no jQuery expert but maybe something like this....untested of course

jQuery

$(document).ready(function() {

    $('#delete').click(function() {

        var table = $('#table').val(); //where #table could be an input with the name of the table you want to truncate

        $.ajax({
           type: "POST",
           url: "truncate.php",
           data: 'table='+ table,
           cache: false,
           success: function(response) {
                alert('table dropped');
            },
            error: function(xhr, textStatus, errorThrown) {
               alert('request failed');
            }
        });

    });
});

PHP (truncate.php)

    try {
        // create a new instance of a PDO connection
        $db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e) {
        // if the connection fails, display an error message
        echo 'ERROR: ' . $e->getMessage();
    }

    $table = $_POST['table'];

    $sql = 'TRUNCATE TABLE '.$mytable;

    $stmt = $db->prepare($sql);
    $stmt->execute();


?>
AdRock
  • 2,959
  • 10
  • 66
  • 106
  • Thank you of answer, what I need to change in PDO connection? Replace DB_NAME, DB_USER and DB_PASS with my data? –  Nov 27 '13 at 13:21
  • Yeah....They are my contants but you just put in your variables like DB_NAME is your database name etc – AdRock Nov 27 '13 at 13:46