Alihamra Alihamra - 1 year ago 73
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 :


if(is_array($_POST['del'])) {
foreach($_POST['del'] as $value){
$query = "TRUNCATE TABLE ".$value."";
$result = mysql_query($query);
echo "Wrong";
$query2 = "DROP TABLE ".$value."";
$result2 = mysql_query($query2);
echo "Wrong2";

As for connect_m.php its simply establishing mysql connection

$con = mysql_connect('localhost', 'root', '');
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("stores_m", $con);


However I've
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 Source

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);
    echo "Error '" . mysql_errno() . "' : " . mysql_error();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download