0

I have a select input where you can choose multiple options. Unfortunately, I have no clue how to insert the values selected into my database. Here is the code I am trying to use, which is giving me an invalid argument passed on my attempt to implode it.

Thanks for any help...

Here is my php:

  $distribs = implode("|", $_POST["distributors"]); 
$distribs  = mysql_real_escape_string($distribs ); 


$distribs = mysql_real_escape_string($_POST['distributors']);
$sql="UPDATE customers SET distributors = '$distribs'
                                            WHERE id='$id'";

Here is my select:

  echo "<select name='distributors' multiple='multiple'>";
9
  • 1
    $_POST["distributors"] isn't an array. Commented Dec 6, 2012 at 3:53
  • what you getting in $_POST["distributors"] ?? Commented Dec 6, 2012 at 3:54
  • what datatype you are using for distributors in DB as implode return strings ?? Commented Dec 6, 2012 at 3:56
  • @alex please enlighten me. my select name is supplosed to be distributors[] rather than just distributors, correct? Commented Dec 6, 2012 at 3:59
  • are you getting the selected value in print_r($_POST['distributors']) Commented Dec 6, 2012 at 3:59

2 Answers 2

1

try like this

echo "<select name='distributors[]' multiple='multiple'>";
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, this is what I had at one point, but for whatever reason, it wasn't working earlier. Now it works! Go figure...I must have had my code wrong elsewhere at the time. Thanks!
1

$_POST["distributors"]; is not an array, implode(); require an array to function,

PHP.net Function for implode();

If your $_POST is a correct array format; then implode will convert the array into a single string.

If your $_POST contains information from a <select> </select> Then the submission would be a single string which can be inserted into a database without the need for your implode(); function.

if you are certain that your $_POST is being submitted in an array format. Use as DeDav Has suggested.

Run this command when your $_POST is populated.

print_r($_POST['distributors'])

If your using a multiple select. Use as again DeDav suggested:

echo "<select name='distributors[]' multiple='multiple'>";

2 Comments

i suspect explode doesnot require an array. here explode — Split a string by string no mention of array
Simple mistake; shouldn't have mentioned explode(); This has now been removed from my post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.