Forum Moderators: coopster

Message Too Old, No Replies

Inserting dynamic form data into a db table

         

omoutop

1:38 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi to all!

I have a script to submit some values into my DB...
the variable type is dynamic as shown below....

how can I get all types in an array in order to store them in one field afterwards?
/////////////////////////////////////////////////
$location = htmlspecialchars($HTTP_POST_VARS['location']);
$town = htmlspecialchars($HTTP_POST_VARS['town']);
$name = htmlspecialchars($HTTP_POST_VARS['name']);
$category = htmlspecialchars($HTTP_POST_VARS['category']);
//////////////////////////////
$Querytype = "select * from Type";
$resulttype = mysql_query($Querytype);
$timestype = mysql_num_rows($resulttype);
$i = 1;
while ($i <= $timestype)
{
$type {"$i"}= $HTTP_POST_VARS['type{"$i"}'];
$i++;
};
////////////////////////////////////////////////////////
echo $location;
echo '<br>';
echo $town;
echo '<br>';
echo $name;
echo '<br>';
echo $category;
echo '<br>';
////////////////////////////////////
$y = 1;
while ($y <= $timestype)
{
echo ${"type$y"};
$y++;
};

thx in advance guys...hope u can help me

jatar_k

3:44 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what's going wrong now?

do you know what part needs to be fixed or can you explain what it is not doing properly?

omoutop

4:33 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi jatar_k...

Nothing is going wrong....I think I wasnt so clear..I am sorry...

////this part get the dynamic variables from the form//////
$Querytype = "select * from Type";
$resulttype = mysql_query($Querytype);
$timestype = mysql_num_rows($resulttype);
$i = 1;
while ($i <= $timestype)
{
$type {"$i"}= $HTTP_POST_VARS['type{"$i"}'];
$i++;
};
////////////////////////////////////////////
and this one is just for testing to display (by echoing) if types are passed to the page///
$y = 1;
while ($y <= $timestype)
{
echo ${"type$y"};
$y++;
};
////

all types are passed and displayed...everything ok...HOWEVER..... how can I store them in a database? .....getting all these dynamic and unpredictable types in one field?might be only one type ($type1) or even 10 ($type1, $type2, ....., $type10)
I need to put them in an array (meybe?) or in a variable $types_all in order to store them in my database field TYPE.......
I need to store them like that ($type1, $type2, ....., $type10)

If I am not clear again plz tell me....

jatar_k

4:36 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hard to answer with out knowing more

can you give an example of what this might output

echo ${"type$y"};

and then the table you want to map it to

omoutop

4:56 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok!

Types are dynamically created by my admin menu...so everytime I want to add an accommodation to my system I have to declare what the type is...soo I have checkboxes for that reason in my form...like:

[] Hotel
[] Apartments
[] Studio etc....all these are not standard but dynamic...so when form is submitted i have to request them by $HTTP_POST_VARS.......as these are not standard I use this type of code to get them:
///////////////////
$Querytype = "select * from Type";
$resulttype = mysql_query($Querytype);
$timestype = mysql_num_rows($resulttype);
$i = 1;
while ($i <= $timestype)
{
$type {"$i"}= $HTTP_POST_VARS['type{"$i"}'];
$i++;
};
//////////////////
it works fine....when I echo them ////
$y = 1;
while ($y <= $timestype)
{
echo ${"type$y"};
$y++;
};
///////////
I get:

HotelApartmentStudio etc....
but in order to store them in one field within the DB (Field called 'type') I need to put all these in an array or something else in ordert to store them like:

Hotel, Apartment, Studio ....and so on and so forth

is there a way to map them all ${"type$y"} in one array?while looping?this is my problemmm

jatar_k

5:02 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you are inserting multiple types into a single column then you should decide on a delimiter to seperate them. That way your scripts will always know how to interpret them.

you create an array here
$type {"$i"}= $HTTP_POST_VARS['type{"$i"}'];

before you insert you could implode [php.net] them using a delimiter. Yourt delimiter could be as simple as a comma or as complicated as a combination of three strange chars, whatever, I can't decide a delimiter for someone else.

Then when the data is selected you can explode [php.net] on that same delimiter.

omoutop

5:15 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thx again!

so if I run

insert into Hotel (type) values ('".$type {"$i"}."')

then this will insert 'HotelApartmentStudio' as it is the script rigt now?

jatar_k

5:15 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



should do

omoutop

5:16 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thx very much...I will try tommorow in my office post again...thx very much for ur help...appreciated!

omoutop

10:07 am on Oct 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unfortunately I cant make this thing to work, if I just echo {"type$y"}; it prints Array which is logical...the only way to see my results is by echoing within a loop...how can I assign all these dynamic variables in ONE in order to insert them in the DB?
plz help me

jatar_k

5:03 pm on Oct 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



did you try imploding them?

omoutop

6:41 am on Oct 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



mope....I am sorry but I think I have to get them all together first in one variable before imploding...isnt it?

jatar_k

2:54 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



aren't you saying they are in an array though? you can implode the array