Forum Moderators: coopster

Message Too Old, No Replies

Multi select posting to mysql

         

jay7981

5:16 am on Jun 22, 2009 (gmt 0)

10+ Year Member



Hey Guys,

i have a form with a multi select list and i want to put the selected values into a single mysql field i have tried the following but it wont put multiples ..

the form:


<select name="skin" size="5" multiple="multiple" id="skin">
<option value="3 Pack Bonus">3 Pack Bonus</option>
<option value="Riptide">Riptide</option>
<option value="Warhammer">Warhammer</option>
<option value="Columbus">Columbus</option>
<option value="Crystal Queen">Crystal Queen</option>
<option value="Halloween">Halloween</option>
<option value="Black Mojo">Black Mojo</option>
<option value="Boneskin">Boneskin</option>
<option value="Wolf Ship">Wolf Ship</option>
<option value="Iron Fist">Iron Fist</option>
<option value="Elcano">Elcano</option>
<option value="Burning Dutchman">Burning Dutchman</option>
<option value="Ghost Dragon">Ghost Dragon</option>
<option value="Ghost Kilamatu">Ghost Kilamatu</option>
<option value="Ghost Kokelau">Ghost Kokelau</option>
</select>

the processor:


//@$skin = serialize($_GET['skin']);
@$skin = join(',',$_GET['skin']);

i have even tried to change from get to post as well ...
any ideas would be nice.

jay7981

6:45 am on Jun 22, 2009 (gmt 0)

10+ Year Member



sorry for double post, i also tried implode(',',$_GET['skin']);

dreamcatcher

6:57 am on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi jay7981,

You need to pass the multiple select options as an array. You do this by using the [] operators after the name, this puts all selections into an array. So..

Change:

name="skin"

to:

name="skin[]"

On insert, do:

implode(',',$_GET['skin']);

or for post vars:

implode(',',$_POST['skin']);

Don`t forget to check if there is at least one selection. ie, one entry in the array. You can`t implode an empty array, so you might see an error.

dc

jay7981

7:40 am on Jun 22, 2009 (gmt 0)

10+ Year Member



thanks so much that worked wonders but now i am running into an issue with this ..


$query = "SELECT * FROM `table`" or die('<br />Select Table-'. mysql_error());
$result = mysql_query($query);
$skin = explode(',',$array['skin']);
if ($result) {

while ($array= mysql_fetch_assoc($result)) {
echo ("<p>&nbsp;</p>");
echo ("<p>Skins:<a href='../images/boats/$skin.jpg' target='_blank'>$skin</a>&nbsp;<em>(Click for image)</em></p>");

What i am trying to do is if there are 2 skins returned then have a link to the name of the skin.jpg, its creating a link ... but its to Array.jpg instead of the data ....

dreamcatcher

9:18 pm on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code isn`t correct as you probably figured. Not 100% certain of what you are trying to do, but this should help:


$query = "SELECT * FROM `table`" or die('<br />Select Table-'. mysql_error());
$result = mysql_query($query);
if ($result) {
while ($array= mysql_fetch_assoc($result)) {
// Explode the array here...
$skin = explode(',',$array['skin']);
// If it has 2 slots, both $skin[0] and $skin[1] have values. So, here you can check for no skins using..
if (empty($skins)) {
// do something..
}
// Or access the slots if available..
echo (isset($skin[0]) ? $skin[0] : '');
echo (isset($skin[1]) ? $skin[1] : '');
}

That should hopefully get you started and enable you to build your links.

dc

jay7981

9:40 pm on Jun 22, 2009 (gmt 0)

10+ Year Member




Not 100% certain of what you are trying to do, but this should help:

as you can see there are quite a few options to select from in the skins category, there is no limit to how many can be selected. when the info is displayed i want to create a link to an image of each skin no matter how many they selected. so i think i need some kind of

 
foreach('skin[]') {
echo "(<a href='../images/$array('skin[]').jpg target='_blank'>$array('skin[]')</a>)";
}
else{
echo "No Skins";
}

//forgive the code i am simply pulling it out of my head without thinking so i know its syntax is not correct.


implementation just not sure where to go ... with it .

dreamcatcher

6:30 am on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (!empty($_POST['skin'])) {
foreach ($_POST['skin'] AS $display) {
echo "<a href='../images/".$display.".jpg target='_blank'>$display</a>";
}
}else{
echo "No Skins";
}

Try that.

dc

jay7981

8:40 am on Jun 23, 2009 (gmt 0)

10+ Year Member



ok this isnt making any sense let me give you the full code that i have and you can see more what i am doing....

Display and Form all on 1 page ...


<?php include("includes/connect.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Boats For Sell</h1>
<p>&nbsp;</p>
<p>
<?php
$query = "SELECT * FROM `table`" or die('<br />Select Table-'. mysql_error());
$result = mysql_query($query);
if ($result) {
while ($array= mysql_fetch_assoc($result)) {
echo ("<p>&nbsp;</p>");
echo ("<p>Seller:$array[seller]</p>");
echo ("<p>Seller Email:<a href='mailto:$array[seller_email]'>$array[seller_email]</a></p>");
echo ("<p>Server:$array[server]</p>");
echo ("<p>Boat Level:<a href='../images/boats/$array[blevel].jpg' target='_blank'>$array[blevel]</a>&nbsp;<em>(Click for image)</em></p>");
echo ("<p>Pirate Level:$array[plevel]</p>");
echo ("<p>Cannons:$array[num_cannons]&nbsp;$array[cannon_size]&nbsp;Pounders</p>");
echo ("<p>Seller:$array[pearls]</p>");
echo ("<p>Seller:$array[gold]</p>");
echo ("<p>Seller:$array[mojo]</p>");
echo ("<p>Elite Pistols:$array[epistols]</p>");
echo ("<p>Elite Cutlasses:$array[ecutlass]</p>");
echo ("<p>Elite Armor:$array[earmor]</p>");
// this is what is not working ...
***********************************************************
echo ("<p>Skins:<a href='../images/boats/$array[skins].jpg' target='_blank'>$array[skins]</a>&nbsp;<em>(Click for image)</em></p>");
***********************************************************
echo ("<p>Premium:$array[premium]&nbsp;Until&nbsp;$array[prem_date]</p>");
echo ("<p>P-Balls:$array[pballs]</p>");
echo ("<p>Hollow Balls:$array[hollowballs]");
echo ('<hr align="center" width="50%" noshade="noshade" />');
}
} else {
echo ("No Boats for sale at this time.");
}
mysql_close;
?>
</p>
<h2>&nbsp;</h2>
<p>&nbsp;</p>
<h1>Sell your Boat!</h1>
<p>&nbsp;</p>
<p>
<form name="sale_frm" action="./sell2.php" method="post">
<table width="100%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td><div align="right">Sellers Name:</div></td>
<td colspan="2"><input type="text" name="seller" id="seller" /></td>
</tr>
<tr>
<td><div align="right">Sellers Email:</div></td>
<td colspan="2"><input type="text" name="seller_email" id="seller_email" /></td>
</tr>
<tr>
<td width="21%"><div align="right">Server:</div></td>
<td colspan="2"><input type="radio" name="server" id="radio" value="East" />
East
<input type="radio" name="server" id="radio2" value="West" />
West</td>
</tr>
<tr>
<td><div align="right">Boat Level:</div></td>
<td colspan="2"><select name="blevel" id="blevel">
<option>Please Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
</select></td>
</tr>
<tr>
<td><div align="right">Pirate Level:</div></td>
<td colspan="2"><select name="plevel" id="plevel">
<option>Please Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
</select></td>
</tr>
<tr>
<td><div align="right">Cannons:</div></td>
<td colspan="2">#:
<input name="num_cannons" type="text" id="num_cannons" size="5" maxlength="3" />
Size:
<input type="radio" name="cannon_size" id="radio3" value="30" />
30lbs
<input type="radio" name="cannon_size" id="radio4" value="50" />
50lbs
<input type="radio" name="cannon_size" id="radio5" value="55" />
55lbs</td>
</tr>
<tr>
<td><div align="right">Pearls:</div></td>
<td colspan="2"><input type="text" name="pearls" id="pearls" /></td>
</tr>
<tr>
<td><div align="right">Gold:</div></td>
<td colspan="2"><input type="text" name="gold" id="gold" /></td>
</tr>
<tr>
<td><div align="right">Mojos:</div></td>
<td colspan="2"><input type="text" name="mojo" id="mojo" /></td>
</tr>
<tr>
<td><div align="right">Elite Pistols:</div></td>
<td colspan="2"><input type="text" name="epistols" id="epistols" /></td>
</tr>
<tr>
<td><div align="right">Elite Cutlass:</div></td>
<td colspan="2"><input type="text" name="ecutlass" id="ecutlass" /></td>
</tr>
<tr>
<td><div align="right">Elite Armor:</div></td>
<td colspan="2"><input type="text" name="earmor" id="earmor" /></td>
</tr>
<tr>
<td><div align="right">Skins:</div></td>
<td colspan="2"><select name="skin[]" size="5" multiple="multiple" id="skin">
<option value="3 Pack Bonus">3 Pack Bonus</option>
<option value="Riptide">Riptide</option>
<option value="Warhammer">Hellhammer</option>
<option value="Columbus">Columbus</option>
<option value="Crystal Queen">Crystal Queen</option>
<option value="Halloween">Halloween</option>
<option value="Dark Mojo">Dark Mojo</option>
<option value="Boneskin">Boneskin</option>
<option value="Wolf Ship">Wolf Ship</option>
<option value="Iron Fist">Iron Fist</option>
<option value="Elcano">Elcano</option>
<option value="Burning Dutchman">Burning Dutchman</option>
<option value="Ghost Dragon">Ghost Dragon</option>
<option value="Ghost Kilamatu">Ghost Kilamatu</option>
<option value="Ghost Kokelau">Ghost Kokelau</option>
</select></td>
</tr>
<tr>
<td><div align="right">Premium:</div></td>
<td colspan="2"><input type="radio" name="premium" id="radio6" value="Yes" />
Yes
<input type="radio" name="premium" id="radio7" value="No" />
No Until:
<input name="prem_date" type="text" id="prem_date" size="12" maxlength="10" /></td>
</tr>
<tr>
<td><div align="right">P-Balls:</div></td>
<td colspan="2"><input name="pballs" type="text" id="pballs" size="15" maxlength="10" /></td>
</tr>
<tr>
<td><div align="right">Hollow Balls:</div></td>
<td colspan="2"><input name="hollowballs" type="text" id="hollowballs" size="15" maxlength="10" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="46%"><input type="submit" name="button" id="button" value="Submit" /></td>
<td width="33%"><input type="reset" name="button2" id="button2" value="Reset" /></td>
</tr>
</table>
</form>
</p>
</body>
</html>

The Processor...


<?php
@$seller = addslashes($_POST['seller']);
@$seller_email = addslashes($_POST['seller_email']);
@$server = addslashes($_POST['server']);
@$blevel = addslashes($_POST['blevel']);
@$plevel = addslashes($_POST['plevel']);
@$num_cannons = addslashes($_POST['num_cannons']);
@$cannon_size = addslashes($_POST['cannon_size']);
@$pearls = addslashes($_POST['pearls']);
@$gold = addslashes($_POST['gold']);
@$mojo = addslashes($_POST['mojo']);
@$epistols = addslashes($_POST['epistols']);
@$ecutlass = addslashes($_POST['ecutlass']);
@$earmor = addslashes($_POST['earmor']);
@$skin = implode(',',$_POST['skin']);
@$premium = addslashes($_POST['premium']);
@$prem_date = addslashes($_POST['prem_date']);
@$pballs = addslashes($_POST['pballs']);
@$hollowballs = addslashes($_POST['hollowballs']);
@$strQuery = "INSERT INTO `table`(`seller`,`seller_email`,`server`,`blevel`,`plevel`,`num_cannons`,
`cannon_size`,`pearls`,`gold`,`mojo`,`epistols`,`ecutlass`,`earmor`,`skins`,
`premium`,`prem_date`,`pballs`,`hollowballs`)
VALUES (\"$seller\",\"$seller_email\",\"$server\",\"$blevel\",\"$plevel\",\"$num_cannons\",\"$cannon_size\",\"$pearls\",\"$gold\",\"$mojo\",\"$epistols\",\"$ecutlass\",\"$earmor\",\"$skin\",\"$premium\",\"$prem_date\",\"$pballs\",\"$hollowballs\")" ;
@$host = "#*$!";
@$user = "#*$!";
@$pw = "#*$!";
@$db = "#*$!";
$link = mysql_connect($host, $user, $pw);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($db, $link);
if (!$db_selected) {
die ('Can not use $db : ' . mysql_error());
}
$result = mysql_query($strQuery);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
mysql_close($link);
echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Thank You</font></p>");
?>

now all of this is working fine with the exception of the link building on the commented line ... Hope this helps you help me LOL ... this is how i am trying to get it to look once displayed

Seller:Raider
Seller Email:email
Server:East
Boat Level:19 (Click for image)
Pirate Level:13
Cannons:200 55 Pounders
Seller:30000000
Seller:90000000
Seller:500000
Elite Pistols:1000000
Elite Cutlasses:1000000
Elite Armor:1000000
Skins:Warhammer(<-Link),Columbus(<-Link),Crystal Queen(<-Link) (Click for image)
Premium:Yes Until 12/13/2009
P-Balls:8970982
Hollow Balls:1235323

dreamcatcher

3:23 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, ok. You are storing the skins in the db as an imploded string. Which might store the info like this:

Halloween,Warhammer,Riptide

When you pull the data, you need to explode it back into an array:

if ($array[skins]) {
$skins = explode(',',$array[skins]);
}

And then build your links. So this try this code:


---
echo ("<p>Elite Armor:$array[earmor]</p>");
// Skins..
if ($array[skins]) {
$skins = explode(',',$array[skins]);
echo "<p>Skins:";
foreach ($skins AS $display) {
echo "<a href='../images/boats/$display.jpg' target='_blank'>$display</a>&nbsp;<em>(Click for image)</em>, ";
}
echo "</p>";
echo ("<p>Premium:$array[premium]&nbsp;Until&nbsp;$array[prem_date]</p>");
---

dc

jay7981

3:44 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



Thanks so much, i had to tweek the code a bit to get it to work, but without you i wouldn't have been able to do it THANKS a TON!

the reason i had to tweek it was becase the if statement was creating havoc ... all though it would be swell for it to echo "No Skins" if the array was empty.

here's the code after the tweek,


echo ("<p>Elite Armor:$array[earmor]</p>");
$skins = explode(',',$array[skins]);
echo "<p>Skins:";
foreach ($skins AS $display) {
echo "<a href='../images/boats/$display.jpg' target='_blank'>$display</a>,";
}
echo "&nbsp;<em>(Click for image)</em></p>";
echo ("<p>Premium:$array[premium]&nbsp;Until&nbsp;$array[prem_date]</p>");

dreamcatcher

6:53 am on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool, glad you got it sorted how you want.

To check if the array is empty, do:


if (!empty($skins)) {
foreach ($skins AS $display) {
echo "<a href='../images/boats/$display.jpg' target='_blank'>$display</a>,";
}
} else {
echo 'No Skins';
}

dc

jay7981

8:07 am on Jun 24, 2009 (gmt 0)

10+ Year Member



ok the script is working with the excetion of it actually echoing "No Skins" when the $skins is empty .. there are no errors, the display looks like this when empty.

Skins:, (click for image)


$skins = explode(',',$array[skins]);
echo "<p>Skins:";
if (!empty($skins)) {
foreach ($skins AS $display) {
echo "<a href='../images/boats/$display.jpg' target='_blank'>$display</a>,";
}
} else {
echo 'No Skins';
}
echo "&nbsp;<em>(Click for image)</em></p>";

dreamcatcher

3:00 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the database, if no skins are selected, is the field blank? I`m guessing that because you aren`t checking initially if the array is empty and you are masking errors (using @), a comma is being entered.

Change this:
@$skin = implode(',',$_POST['skin']);

to this:
@$skin = (!empty($_POST['skin']) ? implode(',',$_POST['skin']) : '');

dc