Kristoff1875 Posted July 6, 2012 Share Posted July 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/ Share on other sites More sharing options...
xyph Posted July 6, 2012 Share Posted July 6, 2012 Why have you used array here? $damage=array($_POST['damage']); Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359553 Share on other sites More sharing options...
Kristoff1875 Posted July 6, 2012 Author Share Posted July 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359554 Share on other sites More sharing options...
xyph Posted July 6, 2012 Share Posted July 6, 2012 Woa woa woa... if you don't understand arrays, why are you trying to insert data into a database? I'll help ya learn it, but it'll involve a lot of reading Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359556 Share on other sites More sharing options...
Kristoff1875 Posted July 6, 2012 Author Share Posted July 6, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359557 Share on other sites More sharing options...
xyph Posted July 6, 2012 Share Posted July 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359562 Share on other sites More sharing options...
Kristoff1875 Posted July 6, 2012 Author Share Posted July 6, 2012 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"? Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359567 Share on other sites More sharing options...
xyph Posted July 6, 2012 Share Posted July 6, 2012 There are a couple ways to do this. The implode function might help You'll probably want to sanitize this data before executing the query. array_walk might help with this. Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359570 Share on other sites More sharing options...
Kristoff1875 Posted July 6, 2012 Author Share Posted July 6, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359571 Share on other sites More sharing options...
Kristoff1875 Posted July 6, 2012 Author Share Posted July 6, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359572 Share on other sites More sharing options...
xyph Posted July 6, 2012 Share Posted July 6, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359573 Share on other sites More sharing options...
Kristoff1875 Posted July 6, 2012 Author Share Posted July 6, 2012 I'm going to blame the fact that it's just turning 6am here as to the reason that I don't really grasp that question completely!! Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359575 Share on other sites More sharing options...
xyph Posted July 6, 2012 Share Posted July 6, 2012 Strings in SQL queries need to be surrounded in quotes. You can't just implode using a comma. Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359577 Share on other sites More sharing options...
Kristoff1875 Posted July 6, 2012 Author Share Posted July 6, 2012 Yep, I know that. Sorry, like I said, it's nearly 6.30am now, I should get some sleep and carry on tomorrow! I did of course know they need quotes, but I couldn't take in what you actually meant! Quote Link to comment https://forums.phpfreaks.com/topic/265287-session-arrays-multiple-select-dropdowns-and-mysql/#findComment-1359578 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.