Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6772581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:33:57+00:00 2026-05-26T15:33:57+00:00

I have a multiple select field designed as below: <select name=deductions[] multiple=multiple> <option>Option 1</option>

  • 0

I have a multiple select field designed as below:

<select name="deductions[]" multiple="multiple">
<option>Option 1</option>
<option>Option 2</option>
<option>.......</option>
</select>

I want to get values from the above select field to db using php post.This is how I have done it

<?php
$deds=$_POST['deductions'];
$deds_joined=join(',',$deds);
$sql=mysql_query("insert into mytable(deduction) values($deds_joined)");
/*code continues*/

My interest is to get the deduction values to an array and store it in db in this manner:
value1,value2,value3.
Unfortunately I have not been able to achieve this.Instead only a single value of the intended array is obtained.
Your assistance will be highly appreciated.Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:33:58+00:00Added an answer on May 26, 2026 at 3:33 pm

    First and foremost:

    Please, don’t use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi – this article will help you decide which. If you choose PDO, here is a good tutorial.

    It has become more important than ever that you follow this advice, ext/MySQL has now been deprecated and using it will emit E_DEPRECATED errors beginning with PHP 5.5, it will one day be completely removed from core language. Having said that, the following applies to all drivers for all databases.

    For the rest of this explanation I shall assume that you are unable to use MySQLi or PDO for some reason (note that the only satisfactory reason here is that they are not available, “I don’t know how to use them” is not an excuse) and that you are forced to use ext/MySQL. If you are able to use either of the newer drivers, then you are able to use prepared statements, and none of this applies. So, with that in mind…

    Next let’s take a look at what is wrong with the previous answer. This centres around escaping user input. It uses mysql_real_escape_string() is a way that really makes no sense whatsoever. This should be used for escaping a single string literal, and absolutely nothing else, ever. It cannot be used to effectively escape numbers, and it cannot be used to escaped parts of SQL that are not only values.

    The following two code snippets show the correct way to do this, dependent on what the data is and, crucially, its type.

    Here’s what we would do if the values are strings (usually a CHAR or VARCHAR field):

    // First create an array of individually escaped values with quotes added
    $deds = array();
    foreach ($_POST['deductions'] as $ded) {
      $deds[] = "'".mysql_real_escape_string($ded)."'";
    }
    
    // Now join them together in an SQL syntax
    $deds_joined = join('), (', $deds);
    
    // Now they can safely be used in the query
    $query = "INSERT INTO mytable (deduction) VALUES ($deds_joined)";
    

    But often in this scenario, the values would simply be numbers, and in this case all we need to do is ensure that PHP is representing them with the correct data type, since they will be automatically safe when they are converted back to strings to be used in the query:

    // First convert the array values to integers
    $deds = array();
    foreach ($_POST['deductions'] as $ded) {
      $deds[] = (int) $ded;
    }
    
    // Now join them together in an SQL syntax
    $deds_joined = join('), (', $deds);
    
    // Now they can safely be used in the query
    $query = "INSERT INTO mytable (deduction) VALUES ($deds_joined)";
    

    This code obviously assumes that the data is of an integer type, floats can easily be handled by simply changing the (int) cast to (float).

    It’s also worth noting that the string approach can be safely and successfully used for numeric values as well, because MySQL will also convert the values to the correct type. But in general it is better and more efficient to pass the data in with the correct type representation within the query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multiple selection SELECT field which I don't want the end user
I have this HTML: <select multiple='multiple' size='3'> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> Using
I have this multiple select: <select id=sel_dest name=dest_var[] multiple disabled=disabled size=10> <option value= selected>Destinatario</option>
I have multiple select field each with multiple options. So, by using jquery to
I have a <SELECT multiple> field with multiple options and I want to allow
I have multiple selects: <select id=one> <option value=1>one</option> <option value=2>two</option> <option value=3>three</option> </select> <select
I have a stored procedure with multiple insert/select statements. Let's say I'm using the
I have I datagrid, on which I want to select multiple rows on a
I have a query which is designed to retireve the name field for all
I have a select field in a form that allows you to select multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.