posting multiple select items to mysql

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
mashamit
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 4:41 pm

posting multiple select items to mysql

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: posting multiple select items to mysql

Post 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" >
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mashamit
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 4:41 pm

Re: posting multiple select items to mysql

Post 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?
mashamit
Forum Newbie
Posts: 5
Joined: Tue Aug 16, 2011 4:41 pm

Re: posting multiple select items to mysql

Post 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 :)
Post Reply