Forum Moderators: coopster

Message Too Old, No Replies

Insert array from form

         

Ann_G

8:04 pm on Sep 17, 2004 (gmt 0)

10+ Year Member



I have been trying for a while to insert an array from a select menu into a table in MYSQL. I tried the serialize function but couldn't get that to work.

I have a file in the MYSQL table named type and I want the name of the type the user selects to go into that field.

My array part of the form looks like this:

<p>Please select a type of incident:
<select name="type[]" multiple>
<option value="disruptive" SELECTED>Disruptive</option>
<option value="accident">Accident</option>
<option value="other">Other</option>
</select>
</p>

The script right now is down to this, (I stripped the serialize function that didn't work):

// Make the array
$type = array('disruptive', 'accident', 'other');
foreach ($type as $key => $value)

//Add the type to the incidents table.
$query = "INSERT INTO incidents (type)
VALUES ('".mysql_escape_string($_POST['type'])."')";

Any help is greatly appreciated.

mincklerstraat

9:35 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// Make the array
$typearray = array();
$type = array('disruptive', 'accident', 'other');
foreach ($type as $key => $value) {
$typearray[] = $_POST[$value];
}
$typeserialized = serialize($typearray);
//Add the type to the incidents table.
$query = "INSERT INTO incidents (type)
VALUES ('".mysql_escape_string($typeserialized)."')";

You may have had troubles serializing since you didn't build up your array with the corresponding $_POST values of your fields (the names of which you put into the array $type). This might work for you. Of course, you have to unserialize this value when you pull it back out of the db, but you probably already knew that.

Ann_G

5:30 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



Thanks!

I've taken a step forward using your help, but I still can't get the name of the selected array element to register in the table. The table field reads a:6:{
no matter which type I select.

I validate using this approach. Any idea what could be missing?

// Check for a type.
$type['disruptive'] = 0;
if ($type['disruptive']) {
echo "true!\n";
}
if (array_key_exists('disruptive', $type)) {
echo "exists!\n";
}

mincklerstraat

6:43 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, Ann_G - had a mistake in my syntax, that might be causing your serialization problem.

// Make the array
$typearray = array();
$type = array('disruptive', 'accident', 'other');
foreach ($type as $key => $value) {
$typearray[$key] = $_POST[$value]; /* this is the line changed - addition of $key inside the first set of brackets */
}
$typeserialized = serialize($typearray);
//Add the type to the incidents table.
$query = "INSERT INTO incidents (type)
VALUES ('".mysql_escape_string($typeserialized)."')";

With $key missing in $typearray, what this was doing is just adding the values to the array as elements without any 'real' keys. Keys should now be restored. Hope it works now.

Ann_G

7:25 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



Thanks for responding so quickly. I added the [$key] just as you suggested but I still get the same result. I must be foing something wrong or not doing something I should be doing. I'm very new at this. I'll keep plodding along, sometimes I figure things out by sheer luck, or trial and error. I appreciate your help nevertheless..

mincklerstraat

10:10 am on Sep 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, round 3 - didn't look at your HTML closely enough the first two times. I'd mistaken your HTML to be doing something else.

Quite simple answer really, you don't have to 'build' the array as the multiple selector named type[] will already come through as an array.

So what you need is this:

if(is_array($_POST['type'])) {
$posttypeserialized = serialize($_POST['type']);

$query = "INSERT INTO incidents (type)
VALUES ('".mysql_escape_string($posttypeserialized)."')";

[ ... ]

}

should you still have some sort of odd problem, you could try conditionally using stripslashes($_POST['type']) before you serialize it if magic_quotes_gpc(); it's been a while since I've used arrays in forms, and I don't know if there would be any issues here with this.

Ann_G

10:12 pm on Sep 20, 2004 (gmt 0)

10+ Year Member



Thanks, and I apologize for not getting back sooner.

I tried what you suggested in round 3 and now I get the word "post" in the table field.

Maybe I'm missing something, I don't quite sure what to put in the brackets with the three periods, so I left that part out.

[ ... ]?
}

mincklerstraat

10:23 pm on Sep 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



brackets often stipulate stuff that you input. Here, it's just whatever you want to with the db query and otherwise, since it's dependent on the conditional statement. Quite right to leave it out if there's nothing else - ellipsis (...) meaning etc. ... and so on ... etc., nothing more than that. Round 4 will have to wait till tomorrow, time for bed and cat is quite insistent on playing with the input devices here.

Ann_G

1:58 am on Sep 21, 2004 (gmt 0)

10+ Year Member



Thank you so very much for all your help. It now works. I realized that I had the array definition before the validation and that must have prevented it from working. Once I deleted that and just used what you gave me, it goes right into the MYSQL table field.
This has been driving me crazy for about a week and with your help I can finally breath again.

//This is what messed it up.
//$type = array
//('disruptive', 'accident','other');

//This works perfectly:
if(is_array($_POST['type'])) {
$posttypeserialized = serialize($_POST['type']);

$query = "INSERT INTO incidents (type)
VALUES ('".mysql_escape_string($posttypeserialized)."')";

mincklerstraat

10:05 am on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very happy to hear it works now - and sorry for not looking at your code more carefully the first times!