0

I would like to create two queries, one to enter the data into one table, another to create a new table. This is my code that creates the new table but does not insert the data. Where am I wrong? Thank you.

$sql = "INSERT INTO progetti(data, ora, nome_progetto)VALUES('".$_POST["data"]."','".$_POST["ora"]."','".$_POST["nome_progetto"]."')";

          "CREATE TABLE $_POST[nome_progetto] (
            id INT(11) AUTO_INCREMENT PRIMARY KEY,
            data date,
            intervento varchar(30),
            descrizione varchar(70),
            ore int(2)
          )";
3
  • 5
    PHP doesn't support multi queries. Btw, please learn about SQL injection and how to prevent them @ bobby-tables.com ... your code is not safe at all and your database could be hacked in a few seconds. Please use prepared statements for user inputs in queries. Commented Sep 27, 2017 at 8:48
  • 1
    You need to separate the two SQL statements. Commented Sep 27, 2017 at 8:48
  • @Twinfriends see mysqli::multi_query Commented Sep 27, 2017 at 9:08

2 Answers 2

1

Here you can create if else statement , if insertion is done then creation will run

<?php

/*
* These are Database Credentials
*/
    $servername = "localhost";
    $username = "root";
    $password = " ";
    $dbname = "test_db";

    /*
    * Intiating the Database connection
    */
    $conn = new mysqli($servername, $username, $password, $dbname);

    /*
    * Checking the Databse connection
    */
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

     $create = "CREATE TABLE ".$_POST[nome_progetto]." (
                    id INT(11) AUTO_INCREMENT PRIMARY KEY,
                    data date,
                    intervento varchar(30),
                    descrizione varchar(70),
                    ore int(2))";
            $result = $conn->query($create);     

    if ($result === TRUE) {
     $sql = "INSERT INTO progetti(data, ora, nome_progetto)VALUES('".$_POST["data"]."','".$_POST["ora"]."','".$_POST["nome_progetto"]."')";

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

    }


    $conn->close();
    ?>
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thank you for your answer, query does not work, you put the full code. The application is installed on a local PC and not connected to the Internet. I'm new to php and some syntax I'm unknown.
$sql = "INSERT INTO progetti (data, ora) VALUES ('".$_POST["data"]."', '".$_POST["ora"]."', )"; $query = $conn->query($sql); $result = $query->result(); if(isset($result)){ $sql = "CREATE TABLE $_POST[nome_progetto] ( id INT(11) AUTO_INCREMENT PRIMARY KEY, data date, intervento varchar(30), )"; $query = $conn->query($sql); $query->result(); }
Hi Max Don't Worry man, Initially, You should install the wamp server in your local system Then there is a folder in C:/ Wamp64/www create file with some sample.php and paste that Code Update the credentials of database in code Then you have to pass the Post values Check it out i have modified the answer
Hi, now everything works perfectly! Thank you for helping me out!
0
  1. Use different SQL statement ( in your case 2, one for insert and other one for create table )
  2. use prepare statements provided by Abstraction Layers ( PDO ) in PHP
  3. read about SQL Injection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.