Alihamra Alihamra - 1 month ago 7
MySQL Question

PHP Truncate and Drop issues using array value

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.

Answer

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();
}