Page 1 of 1

? serialize data into an array and store them into a mySQL ?

Posted: Thu Sep 01, 2005 4:20 am
by phpForX
Hi there,
I need some help again.

Here's my issue:

Let's say, we have a xhtml form with some form elements (eg. input, radio, checkbox etc..).
I wanna process that form with method=post.

Values comming from that form should be written into an array, witch itself should be stored into a mySQL database field.

How can this clever be done.

Furthermore I need to get those data back from the database to display them somewhere else.

How you would do that as well?


This is any XHTML form (only for example..)

Code: Select all

<form class="fmailShop" method="post" name="userform"  action="/path/..">
 <fieldset>

    <legend> form </legend>

        Please insert your Details here:
           <p>
       	    <label for="userTitle"> Anrede : </label>
              <select id="userTitle" name="strAnrede">
                <option value="mr">Mr</option>
                <option value="mrs">Mrs</option>
                <option value="" selected="selected" />
              </select></p>
           <p>
       	      <label for="userForeN"> Forname : </label>
              <input size="24" id="userForeN" type="text" name="strFirstName" value="" />
          </p>
           <p>
       	      <label for="userName"> Surename : </label>
              <input size="24" id="userName" type="text" name="strLastName" value="" />
           </p>
           <p>
       	      <label for="userComp"> Company : </label>
              <input size="24" id="userComp" type="text" name="strCompany" value="" />
           </p>
            <p>
       	      <label for="userUst"> UST.-ID : </label>
              <input size="24" id="userUst" type="text" name="ustID" value="" />
           </p>
            <p>
       	      <label for="userStr"> Street : </label>
              <input size="24" id="userStr" type="text" name="strStreet" value="" />
            </p>
            <p>
       	      <label for="userPlz"> Zip : </label>
             <input size="6" id="userPlz" type="text" name="intPLZ" value="" />
           </p>
            <p>
       	     <label for="userCity"> City : </label>
             <input size="12" id="userCity" type="text" name="strOrt" value="" />
            </p>
            <p>
       	     <label for="userEmail"> Email : </label>
             <input size="24" id="userEmail" type="text" name="strEmail" value="" />
           </p>
	    <p>
       	    <label for="userTel"> Phone : </label>
            <input size="24" id="userName" type="text" name="phoneCompany" value="" />
           </p>
            <input size="24" id="userName" type="submit" name="submit" value="Submit" />

    </fieldset>
 </form>

Posted: Thu Sep 01, 2005 5:32 am
by raghavan20
as you shd be knowing $_POST itself is an array, you can assign this array to another or you can directly use this in your insert query.

Code: Select all

if (isset($_POST)){//you can use this if you want to copy everything thats in the $_POST super global array
	$tempArray = $_POST;
	mysql_query("insert into your_table values ('other_field', '{tempArray}')");
}


or 


if (isset($_POST)){//you can use this if you only want to use a few fields from the POST array
	$tempArray = array();
	$counter = 0; //array index
	$tempArray[counter++] = $_POST["field1"];
	$tempArray[counter++] = $_POST["field2"];
	$tempArray[counter++] = $_POST["field3"];//keep goin until the last field
	mysql_query("insert into your_table values ('other_field', '{$tempArray}')")
}

store the data into an array and serialise/unserialize them

Posted: Thu Sep 01, 2005 6:02 am
by phpForX
Thank you, Raghavan.

I need to put all values comming from that form into an array. Let's say the form should tranfer user information.

Now, what I need is to store that information serialized into a mySQL DB-field, and gat them back unserialized whenever I want... That implies using the serialize() and unserialize() - funktion.

What would be the best way to do that. Would you store the values first into $_SESSION and then into mySQL, or is the $_SERVER better?

Now thats my problem. How would I have to do that and furthermore, how could I grap certain values out of that unserialized array. Let's say if I had to get only the username out of that array?

Gradually listed:

- html form
- values to be stored into an array
- function that serialize that array and store that data into a mysql DB
- function that get the information back into usable variables.

could this somehow be done?

Thanks

Jan



Thanks
Jan

Re: store the data into an array and serialise/unserialize t

Posted: Thu Sep 01, 2005 9:35 am
by JAM
phpForX wrote:I need to put all values comming from that form into an array. Let's say the form should tranfer user information.
The $_POST is allready an array() of whats yuo need.
phpForX wrote:Now, what I need is to store that information serialized into a mySQL DB-field, and gat them back unserialized whenever I want... That implies using the serialize() and unserialize() - funktion.

What would be the best way to do that. Would you store the values first into $_SESSION and then into mySQL, or is the $_SERVER better?
See above. It is allreay at hand to serialize($_POST).
phpForX wrote:Now thats my problem. How would I have to do that and furthermore, how could I grap certain values out of that unserialized array. Let's say if I had to get only the username out of that array?

Gradually listed:

- html form
- values to be stored into an array
- function that serialize that array and store that data into a mysql DB
- function that get the information back into usable variables.

could this somehow be done?
If you are just massstoring information, adding the serialized data in the database as is would work.
If you are storing the data that is bound to something (say a user) you also need to have a key so that you can find the correct data. Or else you would have issues when trying to fetch say a username later.