Akash
Akash

Reputation: 220

how to post multiple values from drop down

I have a html form with multiple choice for values. When i post the values the values must be stored in the database table under the column as text separated by a comma. But it throws an error as below. Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1','Mobile hardware, Android','Android SDK', 'Hindi','Yes','Yes','', '1234')' at line 2

My HTML:

  <label for="cc">Any 2 Certification courses</label>
   <input name="cc" type="text" placeholder="course1,course2">
       <br>

                         <label for="pls">Programming Language Skills</label>
                            <select multiple name="pls" class="pure-input-1-2">
                                <option>C</option>
                                <option>C++</option>
                                <option>Java</option>
                                <option>HTML</option>
                                <option>Javascript</option>
                                <option>CSS</option>
                                <option>PHP</option>
                                <option>SQL</option>
                                <option>.NET</option>
                                <option>Android SDK</option>
                                <option>others</option>
                            </select>   
        <br>

my php script:

 $pls = mysqli_real_escape_string($con, $_POST['pls']);// here pls(prog lang skills) is a column in table with the type text, do i have to make it a varchar? 

$sql="INSERT INTO $tbl_name (name, regno,email,phone,dept,yop,gender,ten,twelve,diploma,cgpa,soa,hoa,cc,pls,cls,night,bpo,marketing,password)
VALUES('$name','$regno','$email','$phone','$dept','$yop','$gender','$ten','$twelve','$diploma','$cgpa','$soa',''$hoa','$cc','$pls',
'$cls','$night','$bpo','$marketing', '$password')";

Upvotes: 0

Views: 1476

Answers (2)

user3524542
user3524542

Reputation:

Your select name should resemble an array in PHP to access multiple values, change your select name as follow:

<select multiple name="pls[]" class="pure-input-1-2">

otherwise you will get only one value.

Upvotes: 2

Mr. Alien
Mr. Alien

Reputation: 157334

You need to loop the array returned by $_POST['pls'] using foreach() like

$str = '';
foreach($_POST['pls'] as $vals) {
    $str .= $vals . ' ';
}

$newstr = mysqli_real_escape_string($con, $str);
echo rtrim($newstr, ' '); //strips out last space

Or strip out the space before you use mysqli_real_escape_string()

Upvotes: 2

Related Questions