0

Hi guys,

I have a mysql database with many tables. Instead of creating a page each to enter info into the relevant tables, is it possible to have a form created dynamically depending on what table I want to insert information into? Database has 20 tables in it at present, but this will most likely increase in time. So one page to insert info into a form that is dynamically created depending on what table is chosen would be one hell of a timesaver!

5
Contributors
6
Replies
29
Views
3 Years
Discussion Span
Last Post by CarterLangley
0

To be honest a “hell of a timesaver” would be also if you let your visitors alter the system tables by their selves. But you don’t want that, why ? . I suppose because you want to have rules of what user can edit what table, what row and in what data formats in each field.
Yes it is possible; there are frameworks out there that have created only for this purpose. I strongly believe that data structure and integrity must stay always at back of the application. You really don’t have any reason to enlighten your users of the backend.

0

This is to make my own life easier. No one but myself uses the database. And it runs locally on my Ubuntu box. I can only learn if I can get help with coding something instead of buying a solution. So who feels like a challenge?

0

Describe your tables in a way that you can create forms and generate queries. A simple way of doing it would be to have table fields stored in an associative array where keys would be field names and values would be field types. That way you can tell whether to use nput field or textarea in the form.

$table = array(
    'tablename' => 'table1',
    'filed11' => 'varchar',
    'field12' => 'smallint',
    'field13' => 'text',
)


$table2 = array(
    'filed21' => 'integer',
    'field22' => 'smalint',
)

// form for the selected table (i.e.table1)
echo "<form>";
foreach($table1 as $key => $value) {
    if($key == 'tablename') {
        echo "<input type=\"hidden\" value=\"$value\">";
    } else {
        switch($value) {
            case 'varchar':
            case 'smallint':
                echo "<input type=\"text\" name=\"$key\">"; break;
            case 'text':
                echo "<textarea name=\"$key\"></textarea>"; break;
        }
    }
}
echo '<input type="submit" name="submit" value="Insert data">';
echo "</form>";

// this is how you process the form
if(isset($_POST['submit'])) {
    unset($_POST['submit']);
    $tablename = mysqli_real_escape_string($_POST['tablename']);
    unset($_POST['tablename']);

    foreach($_POST as $fieldname => $fieldvalue) {
        $fieldNamesArray[] = mysqli_real_escape_string($fieldname);
        $fieldValuesArray[] = mysqli_real_escape_string($fieldvalue);
    }

    $query = "INSERT INTO $tablename ("`. implode('` ,`', $fieldNamesArray) "`) VALUES (" . implode("','", $fieldValuesArray) . "')";

    // then execute the query
    ...
}

This is just a concept. It can be much improved but you get the idea.

Edited by broj1

0

If you want a form for updating data in your tables why not use phpmyadmin or another GUI?

0

Busy using phpmyadmin. Takes longer than doing it through the website though. That code above I can use and probably combine with an idea I have. Will see how I get on over the coming weeks.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.