skip to Main Content

I am trying to create a new table in a database each time a user submits a form. I take the information they submitted and use that to name the new table I create.
However when I query the database to make a new table, I get no response and the table is not made.
The SQL command I am submitting comes directly from when I manually made a table on PHPMyAdmin.

Ive checked to see if the post request was not getting through but it is, the problem has to be that it can’t connect to the database, but I am able to connect to other databases in the system with the same credentials no problem.

<?php

session_start();

require_once('config.php');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

$name = $_POST['user'];
$league = $_POST['league'];
$delim = '_';
$tablename =  $name . $delim . $league;

$s = "CREATE TABLE IF NOT EXISTS '$tablename' 
 ( `id` int(10) NOT NULL ,
  `title` VARCHAR(120) NOT NULL ,
  `description` TEXT NOT NULL ,
  `position_order` INT(11) NOT NULL ,
   PRIMARY KEY (`id`)
   ) ENGINE = InnoDB DEFAULT CHARSET=utf8 ";


$result = mysqli_query($con, $s);
echo $result;




?>

Output is always blank no table is created

2

Answers


  1. Chosen as BEST ANSWER

    Fixed the problem, but Im not sure what the issue was. I am now connecting but not choosing the database, then I updated my SQL command to

    "CREATE TABLE `test`.`$tablename` ( `id` INT(10) NOT NULL , `title` VARCHAR(120) NOT NULL , `description` TEXT NOT NULL , `position_order` INT(11) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB";
    

  2. Try like below. if it doesn’t work properly make sure your database connection working properly.

    <?php
    
    session_start();
    
    require_once('config.php');
    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
    
    $name = $_POST['user'];
    $league = $_POST['league'];
    $delim = '_';
    $tablename =  $name . $delim . $league;
    
    $s = "CREATE TABLE IF NOT EXISTS '$tablename' 
    (id int(10) NOT NULL ,
      title VARCHAR(120) NOT NULL ,
      description TEXT NOT NULL ,
      position_order INT(11) NOT NULL ,
      PRIMARY KEY (id)
    ) ENGINE = InnoDB DEFAULT CHARSET=utf8";
    
    
    $result = mysqli_query($con, $s);
    echo $result;
    
    
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search