Create MySQL table with PHP variable

后端 未结 1 1806
醉话见心
醉话见心 2021-02-11 01:47

I\'m trying to create a table whose name is the value of what is stored inside the variable $name. I have tried numerous different methods but none seem to work for

1条回答
  •  醉梦人生
    2021-02-11 02:27

    Use backticks around table name, not quotes. And escape the input! Also, while this works on localhost, make sure that the user running on your production server has the privilege to CREATE tables (usually it's not, AFAIK, on shared hostings of course).

    A word of warning: are you really sure you want to create a table on a user input?? how many tables are you going to create in this way? Can't you just redesign the whole thing so that you insert values instead?

    $name = mysql_real_escape_string($_POST['name']);
    mysql_query("CREATE TABLE `".$name."` ( name VARCHAR(30), age INT, car VARCHAR(30))");
    

    0 讨论(0)
提交回复
热议问题