Home Ask Login Register

Developers Planet

Your answer is one click away!

Digitlimit February 2016

How to store data from form builder in database

I am trying to create a form builder that will enable users generate survey form/page.

After the form is generated the form attribute is stored on a table.

Question 1: Where can I store the form attribute knowing fully well that the number of fields user might add in the form is unknown.

Question 2: Where and how do I store data submitted through the generated form when some one for example completes the survey form.

Should I create new tables on fly for each of the form attributes? If yes what if over a million forma are created which translates to a million tables.

Is this where multi-tenancy comes into play.

Please provide your answer based on best practices.

Answers


Jimmy Canadezo February 2016

I think I get what you're asking, but why not create one table with 100 columns, labelled 1-100. Set a limit on the amount of fields a user can create(Limit 100). Then, POST the fields and add a sql query to store the values...?

COMMENT ANSWER If the user is already signed in filling this form I would personally do the POST request on the same page.

<?php if (isset($_POST['field1'])){

    $valueforField1 = $_POST['field1'];
    $valueforField2 = $_POST['field2'];
    $valueforField3 = $_POST['field3'];
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "INSERT INTO Survey (field1, field2, field3) // I guess you would `have to add 100 fields here to accommodate the fields in your DB Also, you can set a default value in MySQL that way if field 56-100 is not set it has a value like 0 or add the value in this php file` 
    VALUES ('$valueforField1', '$valueforField2', '$valueforField3')";

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

    $conn->close(); ?>

COMMENT ANSWER or if you want to wait for the user to log in you can store all the values in a SESSION variable.

<?php
session_start();
if (isset($_POST['field1'];)){
if (!(isset($_SESSION['email']))){
$_SESSION['field1'] = $_POST['field1'];
// You would have to do 100 of these
}
}
?>

Post Status

Asked in February 2016
Viewed 3,052 times
Voted 4
Answered 1 times

Search




Leave an answer


Quote of the day: live life