多个下拉列值插入单行而不是多行 [英] 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
预期结果:
id game
1 cricket
2 football
代码 -
<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>
推荐答案
它将它们全部插入一行,因为你正在摧毁你的 $ _ POST ['游戏']
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')");
你需要遍历每个 $ _ POST ['game']
value
You need to loop over each $_POST['game']
value
foreach($_POST['game'] as $choice){
mysql_query("insert into tb values('','$choice')");
}
注意,您的查询是打开SQL注入,确保在插入数据库之前清理数据。此外,不推荐使用 mysql _ *
,因此您应该更新为 mysqli
或 PDO
- http://php.net/manual/en/mysqlinfo。 api.choosing.php
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
这篇关于多个下拉列值插入单行而不是多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!