desithugg Posted April 2, 2006 Share Posted April 2, 2006 inserting data into db from a multiple option form like <form action="trade1.php" method="post" multiple='multiple'> <select name="ids1" multiple="multiple" size="20"><option value='"1' selected>number 1</option><option value='2' selected>number 2</option></select></form> Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/ Share on other sites More sharing options...
Javizy Posted April 2, 2006 Share Posted April 2, 2006 [!--quoteo(post=360970:date=Apr 2 2006, 09:48 PM:name=desithugg)--][div class=\'quotetop\']QUOTE(desithugg @ Apr 2 2006, 09:48 PM) [snapback]360970[/snapback][/div][div class=\'quotemain\'][!--quotec--]inserting data into db from a multiple option form like <form action="trade1.php" method="post" multiple='multiple'> <select name="ids1" multiple="multiple" size="20"><option value='"1' selected>number 1</option><option value='2' selected>number 2</option></select></form>[/quote]Even though it has multiple options, only one name/value pair will be sent to trade1.php.So simply you would do:[code]/* Get value */$ids1 = $_POST['ids1'];/* Insert into DB */$conn = mysql_connect($dbHost, $user, $password);mysql_select_db($database) or die("Unable to select DB " . $database);query("INSERT INTO myTable (isd1) VALUES ( " . $ids1 . ");";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23251 Share on other sites More sharing options...
fenway Posted April 2, 2006 Share Posted April 2, 2006 Really? Why wouldn't PHP put this into an array? Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23257 Share on other sites More sharing options...
wickning1 Posted April 2, 2006 Share Posted April 2, 2006 It's really difficult to help you without a more descriptive explanation of the problem.In response to what Javizy posted, if you're using PHP you need to name your select field "ids[]" in order to get the data back out on the PHP end. For instance:[code]<select name="ids[]" multiple="multiple" size=4><option value="1">Number 1</option><option value="2">Number 2</option><option value="3">Number 3</option><option value="4">Number 4</option></select>[/code]Then in your PHP code:[code]$id_array = $_POST['ids'];foreach ($id_array as $id) { ...INSERT a record}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23259 Share on other sites More sharing options...
Javizy Posted April 2, 2006 Share Posted April 2, 2006 Sorry ignore my post, I was explaining how to use single select boxes. Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23283 Share on other sites More sharing options...
desithugg Posted April 3, 2006 Author Share Posted April 3, 2006 [!--quoteo(post=361022:date=Apr 2 2006, 07:04 PM:name=Javizy)--][div class=\'quotetop\']QUOTE(Javizy @ Apr 2 2006, 07:04 PM) [snapback]361022[/snapback][/div][div class=\'quotemain\'][!--quotec--]Sorry ignore my post, I was explaining how to use single select boxes.[/quote]kk thnx alot i think this really helps i was wondering why alot of the forms i looked at had that [] thing Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23311 Share on other sites More sharing options...
desithugg Posted April 3, 2006 Author Share Posted April 3, 2006 [!--quoteo(post=361022:date=Apr 2 2006, 07:04 PM:name=Javizy)--][div class=\'quotetop\']QUOTE(Javizy @ Apr 2 2006, 07:04 PM) [snapback]361022[/snapback][/div][div class=\'quotemain\'][!--quotec--]Sorry ignore my post, I was explaining how to use single select boxes.[/quote]umm back it seems to insert the data but it inserts multiple rows like if i select 4 options it adds for rows in the db not 4 options in the same row and it inserts "array" as every value in the dbthis is what it looks likeFORM[code]<?PHP$reqlevel = 1;include("membersonly.inc.php");?><form action="trades2.php" method="POST"><select name="ids[]" multiple="multiple" size=4><?php$link = mysql_connect('localhost', 'username', 'password');if (!$link) { die('Could not connect: ' . mysql_error());}$db_selected = mysql_select_db('tpf');if (!$db_selected) { die('Could not select database: ' . mysql_error());}$query = "SELECT pokemon,poke_id,level FROM pokemon WHERE trainer = 'desithugg'";$result = mysql_query($query);if (!$result) { die('Query failed: ' . mysql_error());}/* fetch rows in reverse order */for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) { if (!mysql_data_seek($result, $i)) { echo "Cannot seek to row $i: " . mysql_error() . "\n"; continue; } if (!($row = mysql_fetch_assoc($result))) { continue; } echo " <option value='". $row['poke_id'] ."' selected>". $row['pokemon'] ."(Level:". $row['level'] .")</option>";}mysql_free_result($result);?></select><tr><td colspan="2" align="center"><input type="submit" name="submit" value="View Stats"></td></tr></form>[/code][b]Action:[/b][code]<?PHP$reqlevel = 1;include("membersonly.inc.php");?><?php$link = mysql_connect('localhost', 'username', 'pass');if (!$link) { die('Could not connect: ' . mysql_error());}mysql_select_db('tpf');$id_array = $_POST['ids'];foreach ($id_array as $id) {mysql_query("INSERT INTO trade (ids1) values ('".$_POST['ids']."')");}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23312 Share on other sites More sharing options...
fenway Posted April 3, 2006 Share Posted April 3, 2006 That's because your running a foreach loop -- if I remember correctly, you should be probably be using explode() to join your array elements together and simply INSERT a single record instead. Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23425 Share on other sites More sharing options...
wickning1 Posted April 3, 2006 Share Posted April 3, 2006 I just wrote that as an example, I didn't mean for you to paste it in. It sounds like you want to do something like this:[code]<?PHP$reqlevel = 1;include("membersonly.inc.php");?><?php$link = mysql_connect('localhost', 'username', 'pass');if (!$link) { die('Could not connect: ' . mysql_error());}mysql_select_db('tpf');$id_array = $_POST['ids'];$ids = implode(',',$id_array);mysql_query("INSERT INTO trade (ids1) values ('".$ids."')");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23516 Share on other sites More sharing options...
fenway Posted April 3, 2006 Share Posted April 3, 2006 I always get explode and implode mixed up -- I much prefer Perl's join/split names. Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23556 Share on other sites More sharing options...
desithugg Posted April 3, 2006 Author Share Posted April 3, 2006 k thnx guys if figured it out and seems to have workedit inserts the data correctly just need a lil help vieing it than im done Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23581 Share on other sites More sharing options...
wickning1 Posted April 3, 2006 Share Posted April 3, 2006 [!--quoteo(post=361298:date=Apr 3 2006, 01:40 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Apr 3 2006, 01:40 PM) [snapback]361298[/snapback][/div][div class=\'quotemain\'][!--quotec--]I always get explode and implode mixed up -- I much prefer Perl's join/split names.[/quote]Yeah, I wrote Perl for about 7 years, PHP for going on 1, I find myself typing join() instead of implode() all the time. It just makes more sense. Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23617 Share on other sites More sharing options...
desithugg Posted April 3, 2006 Author Share Posted April 3, 2006 [!--quoteo(post=361359:date=Apr 3 2006, 05:42 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Apr 3 2006, 05:42 PM) [snapback]361359[/snapback][/div][div class=\'quotemain\'][!--quotec--]Yeah, I wrote Perl for about 7 years, PHP for going on 1, I find myself typing join() instead of implode() all the time. It just makes more sense.[/quote]wow you guys are old im only 14 and have been around web languages for about 1 month Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23619 Share on other sites More sharing options...
fenway Posted April 4, 2006 Share Posted April 4, 2006 Not old, just experienced. BTW, I think they've aliased the join() function in PHP as well, finally. Quote Link to comment https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23714 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.