-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"

  • 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? – pcalkins Oct 31 '19 at 20:02
  • You cannot store arrays on MySQL databases. You would probably need to [json_encode](https://www.php.net/manual/en/function.json-encode.php) it before storing. However, as @pcalkins suggested, it's better to properly create some sort of relationship... – Lucas Arbex Oct 31 '19 at 20:04
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). 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](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Oct 31 '19 at 20:07

1 Answers1

-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);
}
  • be sure to use parameterized prepared statements so that you are not vulnerable to injection attacks: https://www.w3schools.com/php/php_mysql_prepared_statements.asp – pcalkins Nov 07 '19 at 21:08