Form to add a new row to table (automatic)
Moderator: General Moderators
Form to add a new row to table (automatic)
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
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
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
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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>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
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>
Last edited by reecec on Tue Jul 25, 2006 6:41 am, edited 1 time in total.
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>
Last edited by Benjamin on Tue Jul 25, 2006 6:58 am, edited 1 time in total.