Jump to content

Multiple Selection Issue


lampquest

Recommended Posts

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

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.

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());

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.