0
<form action="4.php" method="POST">
<select name="select2[]" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
<select name="select[]" multiple>
  <option value="volvo1">Volvo1</option>
  <option value="saab2">Saab2</option>
  <option value="opel3">Opel3</option>
  <option value="audi4">Audi4</option>
</select>
<input type="submit" name="sub" value="submit">
</form>

4.php

<?php
mysql_connect("localhost", "root", "");
$db=mysql_select_db("test");
if(isset($_POST['sub'])) {

$r=$_POST['select2'];
$f=$_POST['select'];


    $val1  = implode("", $r);


    $val2  = implode("", $f);


    $r=mysql_query("insert into test1 (test,test1) values ('$val1','$val2') ");
}


?>

I am working in a php language . I am using select multiple and trying to add the values in my database but all the values in one row only

4
  • What exactly is the issue here? Are you wanting to insert multiple rows? Is it failing to insert the single row? Please elaborate. Commented Sep 22, 2013 at 17:53
  • Well, you're only executing one INSERT statement on the database, so it's only going to insert one row. You're also explicitly using implode() to turn the arrays into single values for a single row. It sounds like what you want to do is loop through the arrays and execute multiple mysql_query() calls, one for each row to be inserted into the database. (Also, you definitely want to look into using mysqli instead of mysql, and using prepared statements and parameterized queries. Currently your code is vulnerable to SQL Injection attacks.) Commented Sep 22, 2013 at 18:10
  • @David please can you give me some examples to explain because i m new in this field Commented Sep 22, 2013 at 18:12
  • My PHP is a bit rusty, but a Google search for "PHP array to database" found this, which may be a place to start: stackoverflow.com/questions/12800279/… Commented Sep 22, 2013 at 18:26

2 Answers 2

1

Try implode(",", $r) instead of implode("", $r):

<?php
mysql_connect("localhost", "root", "");
$db=mysql_select_db("test");
if(isset($_POST['sub'])) {

$r=$_POST['select2'];
$f=$_POST['select'];

    $val1  = implode(",", $r);// to save it as string

    $val2  = implode(",", $f);

    $r=mysql_query("insert into test1 (test,test1) values ('$val1','$val2') ");
}

// and to retrieve data 
explode(",", $result); // to convert it from string to array

?>

Sign up to request clarification or add additional context in comments.

Comments

0

You have to make it comma separated as @jetawe said but I think his query won't work. I suggest you make it in two queries to keep it simple :

   $val1  = implode("', '", $r);  // add quotes
   $val2  = implode("', '", $f);

   $result_1 = mysql_query("insert into test1 (test) values ($val1) ");
   $result_2 = mysql_query("insert into test1 (test1) values ($val2) ");

But I have to mention that naming the variable should be done better to avoid confusion. Make some effort in naming the variable to decrease the errors in your code.
Also you have to use another way for connecting and querying the database. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

Comments

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.