-1

I make a form like this

    <input type="text" name="id">
    <input type="text" name="brand">

and there is a multiple choice select:

    <select name="cars" multiple required>
    <option value="Avanza">Avanza</option>
    <option value="Xenia">Xenia</option>
    <option value="Colt">Colt</option>
    </select>

after i get selected options, i have to pass them to a table in the database; table consists of 3 columns like this: https://i.stack.imgur.com/bBSIJ.jpg id, brand and cars. When I choose all of selected options like this : https://i.stack.imgur.com/codXQ.jpg then I pressed the submit button. How to transform the data into this : https://i.stack.imgur.com/9ixsY.jpg Can you help me, please..

this is a query:

$query = "INSERT INTO cars (id, brand,cars) VALUES ('$_POST[id]', '$_POST[brand]','$_POST[cars]')";
  • To do that you need to loop through each element in $_POST[cars] and insert into database also your code is vulnerable to sql injection, I strongly suggest you to use prepared statements. – NullPoiиteя Oct 30 '15 at 10:01
  • @NullPoiиteя it is not what i mean sir, i want to send data to the database with different "cars" refers to what i choose in multiple selected option and same "id"&"brand". – Nasihun Amin Suhardiyan Oct 30 '15 at 10:18

1 Answers1

-1
$id = $_POST['id'];
$brand = $_POST['brand'];
$cars = $_POST['cars'];
$query = mysql_query("INSERT INTO `cars` (`id`, `brand`,`cars`) VALUES ('".$id."', '".$brand."','".$cars."')");
Krishna Gupta
  • 695
  • 4
  • 15