如何制作SQL表的PHP变量名

时间:2018-07-20 09:08:20

标签: php mysql sql

我有一个html表单,其中一个人填写了输入字段。然后将输入字段发送到PHP表单处理程序。然后,表单处理程序将对其进行处理,并且html表单中输入的变量将成为SQL表的名称。一切正常,除了使变量成为SQL表的名称之外。

看看我的代码:

<?php error_reporting(E_ALL); ini_set('display_errors', 1);?>
<?php $title =$_POST['myfile']?>
<?php $info =$_POST['info']?>
<?php $tags =$_POST['tags']?>
<?php $category =$_POST['category']?>
<?php $allowcomments =$_POST['allowcomments']?>
<?php $flagging =$_POST['flagging']?>
<?php $visibility =$_POST['visibility']?>

<?php $date =$_POST['date']?>
<?php $name =$_POST['name']?>
<?php $size =$_POST['size']?>
<?php $type =$_POST['type']?>
<?php $path =$_POST['path']?>
<?php $sub =$_POST['sub']?>
<?php $cap =$_POST['cap']?>


<?php
$servername='localhost';
$username='root';
$password='you aint gonna know my password!!';
$dbname = "galaxall";

$conn = new mysqli($servername, $username, $password, $dbname);
@mysql_select_db('galaxall');

?>

<?php $title =$_POST['myfile']?><br>
<?php echo $title?><br>
<?php echo $info?><br>
<?php echo $tags?><br>
<?php echo $category?><br>
<?php echo $allowcomments?><br>
<?php echo $visibility?><br>
<?php echo $flagging?><br>

<?php echo $cap ?>
<?php  echo $date ?>
<?php  echo $name ?>
<?php echo $size ?>
<?php  echo $type ?>
<?php echo $sub ?>
<?php echo $cap ?>

<?php $file=$_POST['myfile']?>

<?php

$sql="INSERT INTO `galaxall_uploads` (`ID`, `Title`, `Producer`, `Description`, `Tags`, `Type`, `Category`, `Allow comments`, `Flag offensive comments`, `Date`, `Visibility`,`Size`,`Path`,`Subtitles_source`,`Captions_source`) VALUES (NULL, '$title', '', '$info', '$tags', '$type', '$category', '$allowcomments', '$flagging', '$date', '$visibility','$size','$path','$cap','$sub')";
$sql2="CREATE TABLE $title `Comments` ( `ID` BIGINT(255) NOT NULL AUTO_INCREMENT , `Commenter` VARCHAR(255) NOT NULL , `Comment` TEXT NOT NULL , `Date/time` DATETIME NOT NULL , `Likes` BIGINT(255) NOT NULL , `Dislikes` BIGINT(255) NOT NULL , `Replies number` BIGINT(255) NOT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB;";

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


?>
<?php
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


if ($conn->query($sql2) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql2 . "<br>" . $conn->error;
}


?>


<html>


The file has been uploaded

</html>

已经声明了var $ title,我什至回显了它。但是当我尝试将其命名为表名(sql2)时,我得到了错误

2018-07-20 New record created successfullyError: CREATE TABLE title `Commentsss` ( `ID` BIGINT(255) NOT NULL AUTO_INCREMENT , `Commenter` VARCHAR(255) NOT NULL , `Comment` TEXT NOT NULL , `Date/time` DATETIME NOT NULL , `Likes` BIGINT(255) NOT NULL , `Dislikes` BIGINT(255) NOT NULL , `Replies number` BIGINT(255) NOT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB;
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`Commentsss` ( `ID` BIGINT(255) NOT NULL AUTO_INCREMENT , `Commenter` VARCHAR(25' at line 1 The file has been uploaded

那么如何使PHP变量成为表的名称?

1 个答案:

答案 0 :(得分:2)

我不想进入数据库设计,但是您面临的错误是关于字符串连接以及表名中有空格的问题:

$sql2="CREATE TABLE `$title_Comments` ( `ID` BIGINT(255) NOT NULL AUTO_INCREMENT , `Commenter` VARCHAR(255) NOT NULL , `Comment` TEXT NOT NULL , `Date/time` DATETIME NOT NULL , `Likes` BIGINT(255) NOT NULL , `Dislikes` BIGINT(255) NOT NULL , `Replies number` BIGINT(255) NOT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB;";

这将解决您在问题中提到的错误

编辑:查看评论,我建议您使用一个表来存储评论,评论的用户ID和他们正在评论的视频的ID。正如我说的,每次都不需要自定义表