php - Multiple dropdown values inserting in a single row not in multiple row - Stack Overflow

In this code when i selected multiple values from dropdown it is stoing in a single row but i like to a

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>

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>
Share Improve this question edited Aug 10, 2013 at 5:11 Sean 12.4k3 gold badges31 silver badges47 bronze badges asked Aug 10, 2013 at 5:04 Moses VicksonMoses Vickson 31 silver badge7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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/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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744644982a4585561.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>