Jump to content

Session Arrays, Multiple Select Dropdowns and MySQL...


Kristoff1875

Recommended Posts

Right, another question!! (nearly the last, if not the last, promise!)

 

I am using a modified version of bsmselect to let people choose 3 different variables and add them in to one, for example:

 

LIST A + LIST B + LIST C

 

Which then when a button is clicked, is added to the bsmSelect.

 

When the form is then submitted, when it gets to the next page i'm doing the following:

 

$damage=array($_POST['damage']);

 

To post the data to a value, which when using

print_r($damage); 

is displaying the full array:

 

Array ( [0] => Array ( [0] => Front Wheel N/S (16) > Curb Mark x 2 [1] => Front Wheel O/S (17) > Paint Peel x 3 [2] => Rear Windscreen (10) > Chip (Large) x 5+ ) )

 

So now i'm totally stuck! I have no idea how I get the array from here, stored in to the session and then sent to the MySQL database. I've tried dozens of things, but it just ends up with me getting an "Array" value in the database, or nothing at all.

 

Any ideas much appreciated.

 

Cheers guys.

Link to comment
Share on other sites

Because to be honest, I have no idea what i'm doing with getting the array to post. I need it to post to one field on the database called "damage". Whenever I tested the value before, I got nothing. And after some research I found a bit of code using that and when I tried it, I actually got the array posted to the next page.

 

Should that just be $damage= $_POST['damage'];? As that's what I had before, and it didn't pass anything at all.

Link to comment
Share on other sites

To answer as simply and honestly as I can, because I need to!!

 

I'm not a total noob to php and I've made databases based on users input before and also pulling data from a database. I know what an array is, but, at least in instances like this I've had very little experience.

 

Always happy to read, look at examples and tutorials and ready to learn!

 

I'm guessing from your shock at the use of the array function that I shouldnt touch that until I'm posting it to the database?

 

Just to clarify, in comparison to a fair few on here, I will be classed as a noob! But I feel I know a bit more than a proper noob!

Link to comment
Share on other sites

It's not really a function, per se. I know the bracket notation is kinda confusing. It's a language construct, and it's used to declare arrays.

 

Since $_POST['damage'] already contains an array, you don't have to declare it as one. If you want to force it to be an array, you can typecast it using $damage=(array)$_POST['damage'];

 

The print_r shows you how the array is structured. Try using echo $damage[0]; and echo $damage[1];

 

More reading...

http://php.net/manual/en/language.types.array.php

Link to comment
Share on other sites

Haha, now I just feel dumb.

 

The difference using echo $damage[0]; and echo $damage; is massive!!

 

My issue with that kind of still stands now though, in that when submitted to the database, it'll still submit as "Array". From my understanding (that may be wrong!) I think I need to extract everything from inside the array before I post it to the database? Or would I just set a foreach statement and say "foreach $damage[] send to this field"?

Link to comment
Share on other sites

I tried the implode function and it just failed on me before.

 

Do I do it just before I send the data to the database? As I'm passing these details to the final page of a multipage form using a session. So at the moment I have:

 

$_SESSION['damage'] =$_POST['damage'];

 

So $_SESSION['damage'] is holding the array. Should I strip it out of there and then work with $damage again?

Link to comment
Share on other sites

Right, whatever i've done differently (Thanks to you!) is now meaning the implode function is working and when echoed is showing a list that i'm seperating with commas. So do I just add "$implodedvalue" to the database where I want the results?

Link to comment
Share on other sites

That's entirely up to you. For the sake of saving a few keystrokes, I'm going to refer to it as $damage in any example code or explanations.

 

Well, what do you have to surround a string in for a query?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.