user9871124
user9871124

Reputation:

How to make PHP variable name of SQL table

I have an html form in which a person fills an input field. The input field will then be sent to the PHP form handler. The form handler will then process it and the variable input in the html form will then become the name of an SQL table. Everything is okay except that part of making the variable the name of SQL table.

Look at my code:

<?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>

The var $title has been declared and I an even echo it.But when i try to make it the name of the table(sql2),I get the error

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

So how do I make a PHP variable the name of a table?

Upvotes: 1

Views: 107

Answers (1)

Lelio Faieta
Lelio Faieta

Reputation: 6669

i don't want to get in your database design but the error you are facing is about string concatenation and an issue on having a space in the table name:

$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;";

this will fix the error you mention in your question

EDIT: looking at the comments I can suggest you to have a single table for comments where you store the comment, the id of the user that does the comment and the id of the video they are commenting on. As I said, no need for a custom table each time

Upvotes: 1

Related Questions