-2

i want to post data from multiple selection (select2 form) data as array to mysql database table

This is my multiple selection form data

<select name="category" class="select2 select2-multiple" multiple="multiple"
 multiple data-placeholder="Choose ...">

<option value="1">Category 1</option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
</select>

<input type="submit" name="submit" value="Create Category"
class="btn btn-primary" class="btn btn-primary form-control">

This is my php script

if(isset($_POST['submit'])){
$category = $_POST['category'];

$insert = "insert into sub_category(category) values ($category)";
//then mysql connection and other code
}

suppose i select 1 and 3 value. when i try to post selected data, it posts to database table only one last value and post to column as 3 but i want to post data as "1","3"

3
  • you could just process or loop through the array... but I'm wondering if your database relationships need to be changed? Did you need a one-to-one, one-to-many, or many-to-many? Commented Oct 31, 2019 at 20:02
  • You cannot store arrays on MySQL databases. You would probably need to json_encode it before storing. However, as @pcalkins suggested, it's better to properly create some sort of relationship... Commented Oct 31, 2019 at 20:04
  • Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data. Escaping is not enough! Commented Oct 31, 2019 at 20:07

1 Answer 1

-1

i have found my answer from this link How To Insert data to database from multiple select list using PHP an MySQL

form code

<select name="category[]" class="select2 select2-multiple" multiple="multiple"
 multiple data-placeholder="Choose ..."> <!-- please see name is in array like 'category[]' -->

<option value="1">Category 1</option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
</select>

<input type="submit" name="submit" value="Create Category"
class="btn btn-primary" class="btn btn-primary form-control">

My PHP Code

if(isset($_POST['submit'])){
$category = $_POST['category'];

$i = implode(',', $category);

$insert = "insert into sub_category(category) values
 ('".mysqli_real_escape_string($con,$i)."')"; //$con is database connection code
$run = mysqli_query($con,$insert);
}
Sign up to request clarification or add additional context in comments.

1 Comment

be sure to use parameterized prepared statements so that you are not vulnerable to injection attacks: w3schools.com/php/php_mysql_prepared_statements.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.