lampquest Posted February 6, 2007 Share Posted February 6, 2007 I have been trying to take values from a multiple select list and write it into mysql database. I need someone to closely enlighten me on how to do that pls. Here is my current code below: <?php $user = $HTTP_POST_VARS["user"]; $address = $HTTP_POST_VARS["address"]; $license = $HTTP_POST_VARS["license"]; $products = $HTTP_POST_VARS["products"]; print "Welcome <b>$user</b><p>\n\n"; print "Your address is:<p>\n\n<b>$address</b><p>\n\n"; print "Your product choices are:<p>\n\n"; foreach ( $HTTP_POST_VARS as $key=>$value ) { if ( gettype( $value ) == "array" ) print "$two_dim_value<br>"; } foreach ( $value as $two_dim_value ) if ( gettype( $value ) == "array" ) { print "$two_dim_value<br>"; include "connect.php"; $sql = "insert into selection (user, address, products) values ('$user', '$address', '$two_dim_value')"; $result = mysql_query($sql); if (!$result) { print "<center><font color='red'>Query Error</font><br><br> Error Inserting Entries !</center>"; } } else { print "<center><font color='red'>Query Success</font><br><br> Sucess Inserting Entries !</center>"; } ?> Link to comment https://forums.phpfreaks.com/topic/37308-multiple-selection-issue/ Share on other sites More sharing options...
arianhojat Posted February 6, 2007 Share Posted February 6, 2007 1st of all, use $_POST ($HTTP_POST_VARS is more old school). So in the html have a <select name="products[]" multiple="multiple">.... then in php part of code... <?php //connect to database $host = "localhost"; $user = "myusername"; $pass = "mypassword"; $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect"); ... $products = $_POST['products']; // refers to the products[] array which returns chosen items in multiple select list foreach($products as $value) { //echo $value; //insert query into database $Query = "INSERT INTO documents.doc_owners ( productID, wantsIt ) VALUES ('. $value .' ,'Y')"; $result = mysql_query($Query) or die ("Error in query: $Query. ". mysql_error()); } ?> you could also build a 1 insert query string by looping through all the select list, and then ryunning that 1 query as well. Link to comment https://forums.phpfreaks.com/topic/37308-multiple-selection-issue/#findComment-178377 Share on other sites More sharing options...
lampquest Posted February 7, 2007 Author Share Posted February 7, 2007 Hello arianhojat, When I implemented your solution, it is only inserting one of the chosen items, maybe I am still doing something wrong. Pls give me some pointers as to a the solution building a query string and inserting such. Here is my current code: <?php //connect to database include "connect.php"; $user = $_POST['user']; $address = $_POST['address']; $products = $_POST['products']; // refers to the products[] array which returns chosen items // in multiple select list foreach($products as $value) { //echo $value; //insert query into database $Query = "INSERT INTO selection (username, address, products) VALUES ('$user','$address','. $value .')"; $result = mysql_query($Query) or die ("Error in query: $Query. ". mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/37308-multiple-selection-issue/#findComment-179088 Share on other sites More sharing options...
Balmung-San Posted February 7, 2007 Share Posted February 7, 2007 Instead of foreach try this: for($i = 0; $i < count($products); $i++) { //everything up to VALUES ...VALUES ('$user','$address','$products[$i]')"; } Link to comment https://forums.phpfreaks.com/topic/37308-multiple-selection-issue/#findComment-179091 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.