创建BBDD @variable表

I would like to create a Table in a BBDD by variable in MYSQL. The problem i have is that the code doesn't work and I am not sure why.

$variable = "xxx_'".$_POST['idtour']."'_xxxx";

    // Create the table
        $sql = "CREATE TABLE $tourname (
                    id_leg VARCHAR(3) NOT NULL COMMENT 'Identificación de la leg del tour, en orden')";         
    //The action
    mysqli_query($link,$sql) or die("Error ".mysqli_error());

I think is a problem with the "_".Any help will be fantastic.

The problem isn't the _. It's the '. "xxx_'".$_POST['idtour']."'_xxxx" will result in xxx_'value'_xxxx, as a possible table name, and ' is not valid in a table name. If you MUST include invalid characters, then you'll need to use the backtick operator.

$tablename = "`xxx_'".$var."'_xxxx`";

That should get you further towards your goal.


On a side note: creating a table based on a user-provided variable is a bad idea. It risks users being able to create some bizarre and destructive behavior, and very often it's better accomplished by adding a column to an existing table. Have you tried adding a column user_idtour?