...

View Full Version : loop inserting optional data?



cgibie
02-06-2007, 03:26 AM
Hey guys, I have a question. I am trying to loop for inserting "optional" datas into database.

For instance, I have a form that has 3 input boxes: Job, Education, Address, and those fields/input boxes are optional, which means the user does not have to enter all of them. If they want to enter all, that's great. But if they want to just enter education, and leave other 2 blanks, that's cool too and so forth.

I am wondering if there's a best way to handle this by inserting the data in to the database, like using loop? I don't want to just use a bunch of if statements in my code. Because there are more than just 3 input boxes.

Thanks

martialtiger
02-06-2007, 04:25 AM
Depending on the method you use for the form, you can try something like this:



$invalidkeys = array('submit'); //Add form field names that you do not want

$qry = "INSERT INTO database (";
foreach($_GET as $key => $value)
{
if (!in_array($key,$invalidkeys))
{
$keys .= "`$key`, ";
$values .= "'$value', ";
}
}
$keys = trim($keys,', ');
$values = trim($values,', ');

$qry .= " $keys) VALUES ($values)";

Of course you want to make sure that your form field names are exactly the same name and case as your table fields.

I'm sure there's other solutions out there but this is one I actually just implemented today at work.

Good luck!

cgibie
02-06-2007, 05:34 AM
Depending on the method you use for the form, you can try something like this:



$invalidkeys = array('submit'); //Add form field names that you do not want
What does this line mean? I dont understand why it's there?

foreach($_GET as $key => $value)
also this line, wha tis key and what is value? I am very bad at array stuff.


Good luck!

Thanks for the help , my form should look like this:

<form method=POST action=path.php>
<input box type=text name=job>
<input box type=text name=edu>
<input box type=text name=address>
<input type=sbumit name=submit value=submit>

martialtiger
02-06-2007, 05:43 AM
In your case then you would do this:


$invalidkeys = array('submit'); //Add form field names that you do not want

$qry = "INSERT INTO database (";
foreach($_POST as $key => $value)
{
if (!in_array($key,$invalidkeys))
{
$keys .= "`$key`, ";
$values .= "'$value', ";
}
}
$keys = trim($keys,', ');
$values = trim($values,', ');

$qry .= " $keys) VALUES ($values)";


$invalidkeys is basically an array of whatever form field names you don't want to include in your query like the 'submit' field.

As for the foreach loop, $_POST is an array and you can access the values like so $value = $_POST['key']. So you're basically just getting the keys (which again should be the table field names) and the form value.

Hope that clarifies it for you.

cgibie
02-06-2007, 07:35 PM
I got this error




Notice: Undefined variable: keys in C:\AppServ\www\php\test_array.php on line 13

Notice: Undefined variable: values in C:\AppServ\www\php\test_array.php on line 14
`test1`, `test2`'Test', 'Test2'


Here is my sample html file

<HTML>
<HEAD>
<TITLE>New Document</TITLE>
</HEAD>
<BODY>
<form method=POST action=test_array.php>
<input type=text name=test1>
<input type=text name=test2>
<input type=submit name=submit value=submit>
</form>
</BODY>
</HTML>

cgibie
02-06-2007, 07:50 PM
oh I went to see my db, and those values were saved correctly, but I dont know why I got those 2 lines message error?

scrupul0us
02-06-2007, 08:00 PM
yea you will get errors since you are trying to concatenate those two variables as such



$keys .= "`$key`, ";
$values .= "'$value', ";


on the first pass its looking to add the values to the existing value of a non-existing variable...

add this after the $invalidkeys line


var $keys;
var $values;


-or-


$keys="";
$values="";

cgibie
02-06-2007, 09:06 PM
Thank you. It works as expected now :)

cgibie
02-07-2007, 01:09 AM
oops, it works with my test script, and when I tried to implement with my real script, I got this error after 1 data has been entered into the db:



ErrorDuplicate entry '' for key 1



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum