multiple size select list to send to mySQL db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

multiple size select list to send to mySQL db

Post by jonnyfortis »

Hello i have made a form that has a select list showing products

Code: Select all

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO beauStock (StockID, ID, SizeID) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['StockID'], "int"),
                       GetSQLValueString($_POST['ID'], "text"),
                       GetSQLValueString($_POST['SizeID'], "text"));

  mysql_select_db($database_beau, $beau);
  $Result1 = mysql_query($insertSQL, $beau) or die(mysql_error());

  $insertGoTo = "size-list.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_beau, $beau);
$query_Recordset1 = "SELECT * FROM beauProd";
$Recordset1 = mysql_query($query_Recordset1, $beau) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_beau, $beau);
$query_Recordset2 = "SELECT * FROM beauSizeList";
$Recordset2 = mysql_query($query_Recordset2, $beau) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

<select id="ID" name="ID">
    <option value="select product name">select product name</option>
    <?php
do { 
?>
    <option value="<?php echo $row_Recordset1['ID']; ?>"><?php echo $row_Recordset1['name']; ?></option>
    <?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
  $rows = mysql_num_rows($Recordset1);
  if($rows > 0) {
      mysql_data_seek($Recordset1, 0);
            $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  }
?>
    </select>

<select name="SizeID">
    <option value="select Size">Select Size</option>
    <?php
do { 
?>
    <option value="<?php echo $row_Recordset2['SizeID']; ?>"><?php echo $row_Recordset2['Size']; ?></option>
    <?php
} while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
  $rows = mysql_num_rows($Recordset2);
  if($rows > 0) {
      mysql_data_seek($Recordset2, 0);
            $row_Recordset2 = mysql_fetch_assoc($Recordset2);
  }
?>
    </select>
an i currently have a select list showing size and can pnly select one at a time. how do i make a multiple select list that when sent will add the data to the correct feilds in the db (this being the feilds in Recordset2)

thanks in advance
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple size select list to send to mySQL db

Post by Celauran »

jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: multiple size select list to send to mySQL db

Post by jonnyfortis »

found out how to do it

at the end of the select name with a [] e.g <select name="SizeID[]" multiple="multiple">

and in the php code add

if (isset($_POST['SizeID'])) {
$_POST['SizeID'] = implode(',', $_POST['SizeID']);
}

this does however put all the content in one field. Im not sure if this is correct
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: multiple size select list to send to mySQL db

Post by califdon »

That's probably not going to give you what you want. Check this out: http://stackoverflow.com/questions/2407 ... box-in-php
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: multiple size select list to send to mySQL db

Post by jonnyfortis »

i have looked at this but i really need to the sizes to be sent to the DB and given there own unqiue ID's
Post Reply