Page 1 of 1

Form to add a new row to table (automatic)

Posted: Mon Jul 24, 2006 12:05 pm
by reecec
Hi all

Is it posible to have a script that will automaticly display a text box for column from a table to add a new row as i will not be able to keep making forms to submit rows to different tables. like PHPMYADMIN

I have a script to automaticly echo the whole table but not one for inserting


thanks reece

Posted: Mon Jul 24, 2006 12:24 pm
by feyd
I have no idea what you are asking.

Posted: Mon Jul 24, 2006 12:28 pm
by reecec
you know when you insert a row to a table in phpmyadmin i need a script that will do the same thing

eg say i have a table with these columns name,age

the script would make a form that would see the column headings (eg. name , age)

So it would look like this but text boxes instead of the lines


Name: _____________
Age: _______________

thanks reece

Posted: Mon Jul 24, 2006 2:01 pm
by reecec
also does anyone know if there is a script where you can type the fields you require in a form and then it creates the table

thanks reece

Posted: Mon Jul 24, 2006 5:18 pm
by RobertGonzalez
You may want to try to gank some of the phpMyAdmin codebase.

Posted: Mon Jul 24, 2006 5:31 pm
by Benjamin
Here is a start..

Code: Select all

<form name="name" method="POST" action="#">
    <table cellpadding="0" border="0" cellspacing="0">
<?php
$resource = mysql_query("DESCRIBE `TableName`");

while ($data = mysql_fetch_assoc($resource))
{
    echo '<tr><td>' . $data['Field'] . '</td>';
    
    if (isset($_POST[$data['Field']]))
    {
        $value = $_POST[$data['Field']];
    } else {
        $value = '';
    }
    
    echo '<td><input type="text" name="' . $data['Field'] . '" value="' . $thisValue . '" /></td></tr>';
    
}
?>
    <tr>
        <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
    </tr>
    </table>
</form>

Posted: Tue Jul 25, 2006 5:42 am
by reecec
thats perfect that does exactly what i need but what about inserting to the DB

how would i make it so that this what ever is entered into the boxes would be added i know how to do this using post and entering the values from a form but the thing is as I dont know what the values are going to be as it is made by the script

is it something like this i get errors as i know this is wrong

Code: Select all

<form name="name" method="POST" action="#">
    <table cellpadding="0" border="0" cellspacing="0">
<?php

$resource = mysql_query("DESCRIBE `profiles`");

while ($data = mysql_fetch_assoc($resource))
{
    echo '<tr><td>' . $data['Field'] . '</td>';
   
    if (isset($_POST[$data['Field']]))
    {
        $value = $_POST[$data['Field']];
    } else {
        $value = '';
    }
   
    echo '<td><input type="text" name="' . $data['Field'] . '" value="' . $thisValue . '" /></td></tr>';
   
}
$sql="INSERT INTO profiles VALUES ('$data['Field']')" ;
$dosql=mysql_query($sql);
?>
    <tr>
        <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
    </tr>
    </table>
</form>

Posted: Tue Jul 25, 2006 6:01 am
by Benjamin
untested..

Code: Select all

<?php

// connect to the database
mysql_connect('localhost', '********', '*******');
mysql_select_db('******');

// define a few variables..
$error = false;
$fields = null;
$values = null;

// test to see if the form was submitted
if ((isset($_POST['submit'])) && ($_POST['submit'] == "Submit"))
{
    // pull all the field names from the profiles table
    $resource = mysql_query("DESCRIBE `profiles`");

    // loop through all of them
    while ($data = mysql_fetch_assoc($resource))
    {
       // check to see if data for this field was posted..
       if ((isset($_POST[$data['Field']])) && ($_POST[$data['Field']] != ''))
       {
           // add it to the variables we set earlier
           $fields .= "`" . mysql_real_escape_string($data['Field']) . "`, ";
           $values .= "'" . mysql_real_escape_string($_POST[$data['Field']]) . "', ";
       } else { // this field is in the table but either wasn't posted or is blank..
           // $error = true; //??
           // $errorText = "You must fill out the " . $data['Field'] . " field.";
       }
    }

    if (!$error) // if there isn't an error
    {
        $fields = substr($fields, 0, -2); // remove the last comma
        $values = substr($values, 0, -2); // remove the last comma
        
        // build the query
        $query = "INSERT INTO `tableName` (" . $fields . ") VALUES (" . $values . ")";
        
        // execute the query
        if (!mysql_query($query))
        {
            // there was an error
            echo "The query: " . $query . " failed with the error " . mysql_error();
        } else {
            // the query didn't fail, unset post so the form is blank again..
            unset($_POST);
        }
    } else {
        // there was an error, don't execute the query, just display the error message
        // echo $errorText;
    }
}

?>

<form name="name" method="POST" action="#">
    <table cellpadding="0" border="0" cellspacing="0">
<?php

$resource = mysql_query("DESCRIBE `profiles`");

while ($data = mysql_fetch_assoc($resource))
{
    echo '<tr><td>' . $data['Field'] . '</td>';

    if (isset($_POST[$data['Field']]))
    {
        $value = $_POST[$data['Field']];
    } else {
        $value = '';
    }

    echo '<td><input type="text" name="' . $data['Field'] . '" value="' . $value . '" /></td></tr>';

}
?>
    <tr>
        <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
    </tr>
    </table>
</form>

Posted: Tue Jul 25, 2006 6:52 am
by reecec
exactly what i needed thank you so much and you even explained it along the way which will help me edit it.

thanks again