[英]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. 首先,请不要使用mysql_*
因为它已被弃用,请改用mysqli_
或PDO
。
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,....
. 这样它会给你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: 假设id
字段是自动递增的,那么更改代码如下:
html code HTML代码
<select name="game[]" multiple="multiple">
<option>Football</option>
<option>Volleyball</option>
...
</select>
php code PHP代码
$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']
它将它们全部插入一行,因为你正在破坏你的$_POST['game']
$choice=$_POST['game'];
$choice1=implode(',',$choice);
mysql_query("insert into tb values('','$choice1')");
You need to loop over each $_POST['game']
value 你需要遍历每个$_POST['game']
值
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. 请注意,您的查询对SQL注入是开放的,请确保在插入数据库之前清理数据。 Also, mysql_*
are deprecated, so you should update to mysqli
or PDO
- http://php.net/manual/en/mysqlinfo.api.choosing.php 此外,不推荐使用mysql_*
,因此您应该更新到mysqli
或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: 在mysql_query语句之前插入以下代码:
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. 如果没有,请回复我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.