Create a new table in the database

asbarabungi

I have a form in a php file. When sending the form, I call a test.php file that checks the validity of the data received and inserts them into a table of my database. I would also like to create a new table in the database, with the name $category_ $username. The file is the following:

<?php

if(isset($_POST['mySubmit'])) {
    $db = mysqli_connect('localhost','root','','DBsito');
    if (!$db)
    {
        die('Could not connect to database: ' . mysqli_error());
    }

    $db_select = mysqli_select_db($db, 'DBsito');

    //Salva il nome del file
    $imagename = $_FILES['icona']['name'];
    //tipo del file
    $imagetype = $_FILES['icona']['type'];
    $imagetemp = $_FILES['icona']['tmp_name'];
    
    //Path dell'upload
    $imagePath = "img/upload/";
    
    if(is_uploaded_file($imagetemp)) {
        if(move_uploaded_file($imagetemp, $imagePath . $imagename)) {
            echo "Sussecfully uploaded your image.";
        }
        else {
            echo "Failed to move your image.";
        }
        
    }
    else {
        echo "Failed to upload your image.";
    }

    $categoria = mysqli_real_escape_string($db, $_POST['categoria']);
    $username = mysqli_real_escape_string($db, $_POST['utente']);

    $result = mysqli_query($db, "SELECT categoria.nome_categoria, categoria.user_utente FROM categoria WHERE BINARY categoria.nome_categoria = BINARY '$categoria' AND BINARY categoria.user_utente = BINARY '$username' ");

    if(!empty($categoria) && mysqli_num_rows($result)) {
        $name_error = "Categoria già esistente!";

    }
    else if (!empty($categoria)){
        $query = "INSERT INTO categoria (nome_categoria, user_utente, icona) values ('$categoria','$username', '$imagename')";
        $db->query("CREATE TABLE '$categoria'_'$username'");

        // sql to create table
        $sql = "CREATE TABLE $categoria'_'$username (
        )";
        
        if ($db->query($sql) === TRUE) {
        echo "Table MyGuests created successfully";
        } else {
        echo "Error creating table: " . $db->error;
        }

        if(!mysqli_query($db, $query)){
            die("DAMMIT");
        }
        else{ 
            { header("Location: confermaCategoria.php"); }
        }
        mysqli_query($db, $query);
    }
    else {
        $name_error = "";
    }
    mysqli_close($db);

}
?>

The data is inserted into the existing table in the database, but I cannot create the new table. How can I do? Where am I wrong?

Barmar

You have incorrect quotes around and in the table name. You also have to specify at least one column in the table. I've made up a couple of columns, you should replace them with the names and types you need.

$sql = "CREATE TABLE `{$categoria}_{$username}` (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
    col1 VARCHAR(100)";

You also need to add validation of $categoria and $username to protect against SQL injection. You can't use a prepared statement with parameters for table/column names, so you must validate them yourself. There's also little point in using mysqli_real_escape_string(), because escaping doesn't work in these names.

In general, if you're creating table names dynamically like this, you have a poor database design. Dynamic information should be in table data, not table/column names.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unable to create a table in new database

Unable To create new table in database

How to create a new table into a database on android studio?

Add new Column to Android SQLite Database Create new table

How to create new database table (if not exist) through VQMOD

Can't create new table in an empty database in intellij idea

How to create a new Table in SQLite database from android app

SQLite Database - Create new tables and set Table name through a popup

How to create a new table in database using sapui5?

create a new sqlite table within the same database that was created during initialmigration

Create Database and table

Create SQLite Database and table

Create a database then table with dockerfile

Cannot create a Table in database

Unable to create a table in database

Create new sql table by entering specific row of specified table in the same database

Create a new table in Python

Create new database with API call

Create new database and tables on the fly

There is no create new database option in phpmyadmin

Create a new database with MySQL Workbench

How can I create a new table in my SQLite Database using a param from a Java String variable?

How to read and create new FoxPro 2.6 database table using Python dbf library

Is it possible we create new column in Database by PHP and automatically show in HTML table?

Create A New Database From Another Database

Azure database: create table if not exists

MySQL database " create table " query

Can't create table in database

Scala Slick Create Table in Database