Reputation: 484
Simply, I would like to truncate then delete a table from a database in mysql as follows :
require_once('connect_m.php');
if(isset($_POST['del'])){
if(is_array($_POST['del'])) {
foreach($_POST['del'] as $value){
$query = "TRUNCATE TABLE ".$value."";
$result = mysql_query($query);
if(!$result){
echo "Wrong";
}
$query2 = "DROP TABLE ".$value."";
$result2 = mysql_query($query2);
if(!$result2){
echo "Wrong2";
}
}
As for connect_m.php its simply establishing mysql connection
<?php
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("stores_m", $con);
?>
However I've echo
the $value
and I got the correct value needed (correct table names), but the code does not truncate or delete any selected table! And the most annoying part is that there are no warning or error messages either for php/mysql.
Upvotes: 1
Views: 197
Reputation: 10084
Replace ' quotes by `. So your query should be like this:
$query = "TRUNCATE TABLE `".$value."`;";
$query2 = "DROP TABLE `".$value."`;";
You also may not use quotes around table names at all.
Upvotes: 1
Reputation: 25602
You need to remove the quotes around your table name for TRUNCATE and DROP to work
Instead of echoing "error" you should display the real error returned by MySQL that would help you understand what happens:
$result2 = mysql_query($query2);
if(!$result2){
echo "Error '" . mysql_errno() . "' : " . mysql_error();
}
Upvotes: 1