0

I am trying to create a table with columns from users form input. a text box creates the database, another textbox creates the tables and using javascript more textboxes can be created, which creates more tables. Now I have a textbox next to the "table" textbox, which can accept entries such as: "id item quote price" The total number of inputs is unknown could be four to six for example, but I am trying to create columns in the tables based on these values. Only one column is created in the database, which is the last value, for example price, but I am trying to get all values into the correct table.

$getMessage = $_POST["dbMessages"];
$questions = $_POST["dbMessagesName"];
$answers = $_POST["dbMessages"];
$combined = array_combine($questions, $answers);    
$dbConnectionT = mysqli_connect($host, $user, $password, $dbName);
        // create loop to assign input boxes together
foreach($combined as $question => $answer)
{           
    $answerSplit = explode(" ", implode($getMessage));
    foreach ($answerSplit as $split)
        {
        // create the tables and columns from the message text box
        $sqlCreateTable = "CREATE TABLE " . $question . "(
        " . $split . " VARCHAR(6)
                        )" ;
                        echo $split;
                        echo "</br>";   
                    }
                    // if the connection and sql query is true echo for debugging   
                    if ($dbConnectionT->query($sqlCreateTable) == TRUE) {
                    print "{$question} = {$answer} ";
                    //echo "made it here";
                    echo "</br>";

                    }
   }

So i am trying to splitthe text box using explode and trying to update the question table with the related answer split. Please note, for this example I understand my violation on the database primary key and not worried about the database key aspects, for this example!!

Thanks in advance

6
  • 2
    Honestly, this design is beyond bad. There is absolutely NO reason to create a table per question. And it's vulnerable to sql injection attacks anyways. And as-written. you're not creating one table with multiple fields, you're building a bunch of sql strings that would create a single field table, but then you OVERWRITE them on every loop iteration, leaving only the LAST create statement. So in effect. no matter how many $split you generate, you will only ever execute ONE create query. Commented Apr 9, 2015 at 14:34
  • This is not a database task, a programming one, this is not going live so no sql injection problems!! It is to read in a message and create the aspects required. I understand databases and the importance of attacks and relations etc. But this is a programming aspect!! Thank you! Commented Apr 9, 2015 at 14:48
  • I am just trying to update the tables create from the question with the related text in the box as the columns! Commented Apr 9, 2015 at 14:51
  • @Seb Regardless of the hideous table design, the "programming aspect" is just as bad. Marc pointed out exactly why even the logic is so flawed it doesn't matter how the DB is structured, the code will never do what you intend it to do. You need to STOP and think about your code, because it's apparent you don't have the slightest idea what it is even doing. Commented Apr 9, 2015 at 15:31
  • Thanks, No i am new to programming, how do i execute all the create queries then please? Commented Apr 9, 2015 at 15:37

2 Answers 2

0

Rather than creating a table for each split (which will only overwrite itself) you should store the data into a JSON string and create a table with the data in the JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thank you for your productive comment, do you have an example for this please?
php.net/manual/en/function.json-encode.php Here you have a documentation about JSON's and some tools to handle it. I would use this answer to put the JSON into the mysql db stackoverflow.com/questions/3199887/… . I hope it helps
0

Working Solution

    $getMessage = $_POST["dbMessages"];
    $questions = $_POST["dbMessagesName"];
    $answers = $_POST["dbMessages"];
    $combined = array_combine($questions, $answers);

$dbConnectionT = mysqli_connect($host, $user, $password, $dbName);


        // create loop to assign input boxes together
        foreach($combined as $question => $answer)
        {

            $columns = "`".str_replace(" ", "` VARCHAR(60), `", $answer)."` VARCHAR(60)"; 
            // create the tables and columns from the message text box
                    $sqlCreateTable = "CREATE TABLE " . $question . "(
                                         " . $columns . "
                                        )";

                            echo "</br>";   


                    // if the connection and sql query is true echo for debugging   
                    if ($dbConnectionT->query($sqlCreateTable) == TRUE) {


                    print "{$question} = {$answer} ";
                    echo "</br>";
                    echo "add messages ";
                    echo "</br>";


            }
        }

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.