Page 1 of 1

posting multiple select items to mysql

Posted: Fri Aug 19, 2011 10:08 am
by mashamit
Good Afternoon,

I have the following code here

form.php (a snippet of it)

Code: Select all

<form action="recipesubmit.php" method="post">
  <br>

  Course
  
    <select name="ccourse">
    <?php
$sql1 = "SELECT course FROM ccourse ".
"ORDER BY course";

$rs1 = mysql_query($sql1);

while($row1 = mysql_fetch_array($rs1))
{
  echo "<option value=\"".$row1['course']."\">".$row1['course']."</option>\n  ";
}
?>
  </select><br><br>
Contributor

  <select name="contributor" multiple>
    <?php
$sql1 = "SELECT contributor FROM contributor ".
"ORDER BY contributor";

$rs1 = mysql_query($sql1);

while($row1 = mysql_fetch_array($rs1))
{
  echo "<option value=\"".$row1['contributor']."\">".$row1['contributor']."</option>\n  ";
}
?>
  </select><br><br>

submitrecipe.php

Code: Select all

<?php

	include 'connect.php';
	
	$title = $_POST['title'];
	$contributor = $_POST['contributor'];
	$client = $_POST['corporate'];
	$fcourse = $_POST['fcourse'];
	$ftype = $_POST['ftype'];
	$book = $_POST['book'];
	$ccourse = $_POST['ccourse'];
	$code = $_POST['code'];
	$program = $_POST['program'];
	$series = $_POST['series'];
	$day = $_POST['day'];
	$week = $_POST['week'];
	$year = $_POST['year'];
	$image= $_POST['image'];
	$link= $_POST['link'];
	
		
	if(!$_POST['submit'])  {
		echo 'please fill out the form';
		header('Location: index1.php');
	} else {
		mysql_query("INSERT INTO Recipes (`id`,`Title`,`Contributor`,`Course`,`Client`,`Year`,`Series`,`Week`,`Code`,`Day`,`Foodcourse`,`Foodcategory`,`Prog`,`Book`,`image`,`Links`)
						VALUES(NULL,'$title','$contributor','$ccourse','$client','$year','$series','$week','$code','$day','$fcourse','$ftype','$program','$book','$image','$link')") or die(mysql_error());
		echo "New Recipe Added";
		header('Location: index1.php');
	}
?>
The drop down list in contributor gets the list from another table within the database, but when I try and post multiple selections, I get just the last item clickedsent across. Im not sure what needs to be changed to allow this to work for this form menu item

Re: posting multiple select items to mysql

Posted: Fri Aug 19, 2011 10:25 am
by social_experiment
The correct use of multiple is

Code: Select all

<select name="contributor" multiple="multiple" >
If i'm not mistaken, contributor needs to be an array for multiple values to be returned

Code: Select all

<!-- 
 now $_POST['contributor'] is an array.
-->
<select name="contributor[]" multiple="multiple" >

Re: posting multiple select items to mysql

Posted: Sat Aug 20, 2011 4:43 am
by mashamit
Many thanks for the swift response, however after changing the script to

Code: Select all

  <select name="contributor[]" multiple="multiple">
    <?php
$sql1 = "SELECT contributor FROM contributor ".
"ORDER BY contributor";

$rs1 = mysql_query($sql1);

while($row1 = mysql_fetch_array($rs1))
{
  echo "<option value=\"".$row1['contributor']."\">".$row1['contributor']."</option>\n  ";
}
?>
  </select>
I now get the word 'array' placed in the mysql column, is there something else I need to change or add to get the selected items in the column?

Re: posting multiple select items to mysql

Posted: Mon Aug 22, 2011 11:48 am
by mashamit
Can anyone suggest a fix for this issue, or an alternative way of moving the information from form --> submitrecipe.php --> database that would work. its driving me a litte nuts :)