Delete table from MySQL database with HTML form and PHP
I have created a database with a table named “test”.
I have created a HTML file with a HTML form called “deletetableform.html”.
I have created a php file called “deletetable.php”.
Here are the codes:
For deletetableform.html:
A HTML form with 1 text field named “temp” and 1 submit button create here. User has to input the table name into the “temp” text field. When the submit button is clicked, the form will direct us to the “deletetable.php” file.
<form action="deletetable.php" method="post"> Enter the table you like to delete from the MySQL database: <input name="temp" type="text" /> <input name="Submit" type="submit" value="delete table" /> </form>
The HTML form:
Enter the table you like to delete from the MySQL database:
For deletetable.php:
First we have to connect to database:
mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed"); mysql_select_db("DataBase Name")or die("Connection Failed");
Then we create a PHP variable to hold the value from the text field “temp” of the HTML form.
$temp = $_POST['temp'];
Now we create the query for deleting the table that named the same as the input from the text field “temp” of the HTML form.
$query = "DROP TABLE $temp";
Now we run the query with the “mysql_query( )” PHP function and to check whether the table has deleted successfully, I have put an “if” condition there to echo out the result.
if(mysql_query($query)){ echo "table deleted";} else{ echo "fail";}
All together for deletetable.php:





