Scriptmaker
05-18-2007, 10:12 PM
<?php
if(isset($_POST['submit']))
{
//Define variables
$keys = "";
$values = "";
$error = "";
// Copy post data into another array
$arr_post_data_copy = $_POST;
//designate which form data need to be handled
// differently, if applicable
$badkeys =
array(
"submit", //neccesary, otherwise will try to input data into
//nonexistant 'submit' column.
"data1",
"etc"
);
//remove these keys from arr_post_data_copy
foreach ($badkeys as $b)
{
unset($arr_post_data_copy[$b]);
}
//build an array with all of the keynames
$count = "0";
while ($count< count($arr_post_data_copy))
{
$goodkeys[] = key($arr_post_data_copy);
next($arr_post_data_copy);
$count++;
}
//build key and value strings with post data
$count = "0";
foreach ($arr_post_data_copy as $data)
{
//display values while building the string
echo $goodkeys[$count]." : ".$data."<br>";
//unnecessary checks to prevent comma at beginning
if ($keys != "")
{
$keys .= ",".$goodkeys[$count];
}
else
{
$keys .= $goodkeys[$count];
}
if ($values != "")
{
$values.= ",'".addslashes($data)."'";
}
else
{
$values.= "'".addslashes($data)."'";
}
$count++;
}
//check if data has made it this far
if ($keys == "" OR $values == "")
{
$error .= "<li>No values were passed to the script.";
}
if ($error == "")
{
// Connect to Database
include("connect.php");
// Execute the query
mysql_query("INSERT INTO tablename ($keys) VALUES ($values)")or die(mysql_error());
echo "Entry # ".mysql_insert_id().
" was successfully added to the database";
}
else
{
echo $error;
}
}
else
{
?>
Test Form:<br />
<form method="POST" name="form">
<input type="text" size="25" name="textbox1" />
<input type="text" size="25" name="textbox2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
?>
I made this script because I've been working on a project that required three separate forms to enter data into three different database tables. I didn't want to have to change the field names and values for each form handler, so I decided make a loop do it for me.
I'm positive it's been done before, but I wanted to see if I could do it on my own.
My own critique:
The script has some limitations, such as:
form field names have to match database column names
you can't have multiple inputs insert only one value into one column (such as multiple checkboxes)
I added a lot of unnecessary bulk by adding all of the checks for empty strings just to make sure a comma wasn't at the beginning. I could have easily just checked for a comma right before querying the db and then removed it.
====
So what do you think? Thanks for your opinions!
p.s. - sorry about the wide code window. I can't figure out which line is making it stretch.
if(isset($_POST['submit']))
{
//Define variables
$keys = "";
$values = "";
$error = "";
// Copy post data into another array
$arr_post_data_copy = $_POST;
//designate which form data need to be handled
// differently, if applicable
$badkeys =
array(
"submit", //neccesary, otherwise will try to input data into
//nonexistant 'submit' column.
"data1",
"etc"
);
//remove these keys from arr_post_data_copy
foreach ($badkeys as $b)
{
unset($arr_post_data_copy[$b]);
}
//build an array with all of the keynames
$count = "0";
while ($count< count($arr_post_data_copy))
{
$goodkeys[] = key($arr_post_data_copy);
next($arr_post_data_copy);
$count++;
}
//build key and value strings with post data
$count = "0";
foreach ($arr_post_data_copy as $data)
{
//display values while building the string
echo $goodkeys[$count]." : ".$data."<br>";
//unnecessary checks to prevent comma at beginning
if ($keys != "")
{
$keys .= ",".$goodkeys[$count];
}
else
{
$keys .= $goodkeys[$count];
}
if ($values != "")
{
$values.= ",'".addslashes($data)."'";
}
else
{
$values.= "'".addslashes($data)."'";
}
$count++;
}
//check if data has made it this far
if ($keys == "" OR $values == "")
{
$error .= "<li>No values were passed to the script.";
}
if ($error == "")
{
// Connect to Database
include("connect.php");
// Execute the query
mysql_query("INSERT INTO tablename ($keys) VALUES ($values)")or die(mysql_error());
echo "Entry # ".mysql_insert_id().
" was successfully added to the database";
}
else
{
echo $error;
}
}
else
{
?>
Test Form:<br />
<form method="POST" name="form">
<input type="text" size="25" name="textbox1" />
<input type="text" size="25" name="textbox2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
?>
I made this script because I've been working on a project that required three separate forms to enter data into three different database tables. I didn't want to have to change the field names and values for each form handler, so I decided make a loop do it for me.
I'm positive it's been done before, but I wanted to see if I could do it on my own.
My own critique:
The script has some limitations, such as:
form field names have to match database column names
you can't have multiple inputs insert only one value into one column (such as multiple checkboxes)
I added a lot of unnecessary bulk by adding all of the checks for empty strings just to make sure a comma wasn't at the beginning. I could have easily just checked for a comma right before querying the db and then removed it.
====
So what do you think? Thanks for your opinions!
p.s. - sorry about the wide code window. I can't figure out which line is making it stretch.