MYSQL问题(创建表)

时间:2014-06-16 02:47:43

标签: php mysql

我使用上一个表单中的信息来创建一个混乱的字符串。我使用此字符串来命名我在下面的代码中创建的表:

// get data that sent from form 
$TN = mysql_real_escape_string($_POST['name']);
$TQ = mysql_real_escape_string($_POST['q']);
$CR = mysql_real_escape_string($_POST['creator']);

//Add Randomness to String
$datetime=date("d/m/y"); //create date time
$R1=rand(5000, 150);
$R2=rand(5000, 10000);
$R3=rand(5000, 15000000);

//Create String called URL
$URL = $TN . $R1 . $TQ . $R2 . $CR . $R3;

//Eliminate Spaces
$URL=str_replace(" ","#%","$URL");

//echo $URL;

//add name of table in a table containing the names of these tables
mysql_select_db("$db_name")or die("cannot select DB");
$sql="INSERT INTO $tbl_name(URL,topic)VALUES('$URL','$TQ')";
$result=mysql_query($sql);

//Create the Table
$sql="CREATE TABLE ".$URL."(Image BLOB,Rating INT(255),Id INT KEY AUTO_INCREMENT)";
$result=mysql_query($sql);

这段代码很棒!只要$ TQ,$ TN或$ CR在单词中包含空格,它就会失败! 以下是错误消息:表必须至少有一列

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您必须用`:

将表名括起来
$sql="CREATE TABLE `".$URL."` (Image BLOB,Rating INT(255),Id INT KEY AUTO_INCREMENT)";