简体   繁体   中英

Multiple dropdown values inserting in a single row not in multiple row

In this code when i selected multiple values from dropdown it is stoing in a single row but i like to apply value in each row so please anybody help me. Actual result:

id    game
1.    cricket,football,tennis

Expecting result:

id         game
1            cricket
2            football

code -

                <html>
                <body>
                  <?php
                     if(isset($_POST['submit']))
                     {
                    $query=mysql_connect('localhost','root','');
                       mysql_select_db("freeze",$query);
                       $choice=$_POST['game'];
                      $choice1=implode(',',$choice);
                      mysql_query("insert into tb values('','$choice1')");
                         }
                      ?>
                        <form method="post" action="multipleselect.php">
                           Select your favourite game:<br/>
                        <select name="game[]" multiple="multiple">
                              <option>Football</option>
                                   <option>Volleyball</option>
                                       <option>Badminton</option>
                                        <option>Cricket</option>
                                           <option>Cricket1</option>
                                             <option>Cricket2</option>
                                             <option>Cricket3</option>
                                                 <option>Cricket34</option>
                    </select>
             <input type="submit" name="submit">
                  </form>
                  </body>
                 </html>

First of all, please don't use mysql_* as it's deprecated, use mysqli_ or PDO instead.

now if you just want the values of options then do it like this

<select name="game[]" multiple="multiple">
      <option value="1">Football</option>
      <option value="2">Volleyball</option>
      ...
</select>

this way it'll give you 1,2,.... . Hope that's what you're looking for.

and if you're looking for query like this

INSERT INTO tb (`game`) VALUES ('Football'),('Volleyball')

assuming that id field is auto-incremented, then change the code as follows:
html code

<select name="game[]" multiple="multiple">
  <option>Football</option>
  <option>Volleyball</option>
  ...
</select>

php code

$choice=$_POST['game'];   
$sql = "INSERT INTO tb (`game`) VALUES ";
$sqlValues= null;
foreach($choice as $ch) {
    $sqlValues .= "('$ch')," ;                     
}
$sql.=rtrim($sqlValues, ",");                      
echo $sql;

this way you could get

id         game
1          cricket
2          football

It is inserting them all in 1 row, because you are imploding your $_POST['game']

$choice=$_POST['game'];
$choice1=implode(',',$choice);
mysql_query("insert into tb values('','$choice1')");

You need to loop over each $_POST['game'] value

foreach($_POST['game'] as $choice){
    mysql_query("insert into tb values('','$choice')");
}

note, your query is open to SQL injection, make sure to sanitize your data before inserting into your db. Also, mysql_* are deprecated, so you should update to mysqli or PDO - http://php.net/manual/en/mysqlinfo.api.choosing.php

Are there any error messages being displayed? Insert the following code just before the mysql_query statement:

echo "insert into tb values('','$choice1')";

what is being displayed?

<?php
if(isset($_POST['submit']))
{
  $query=mysql_connect('localhost','root','');
  mysql_select_db("freeze",$query);
  $choice=$_POST['game'];
  $choice1=implode(',',$choice);
  foreach($choice1 as $choice) 
  {
     mysql_query("insert into tb values('','$choice')");
  }
}
?>

You just try this code i hope it works for you. If not then please reply me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM