我在PHP中有这个变量:
$game_rate = implode(",",$_POST['game_rate']);
$game_opt = implode(",",$_POST['game_opt']);
现在我想将它插入MySQL:
INSERT INTO t_game(game_rate, game_opt) VALUES('$game_rate', '$game_opt')
数据显示在表格中:
game_rate | game_opt
8,5 | 7,5
我想要的是,是否可以在每一行中插入数据。所以它可以是这样的:
game_rate | game_opt
8 | 7
5 | 5
答案 0 :(得分:1)
可以使用单个查询来完成。只需使用多个值创建查询即可。试试 -
"INSERT INTO t_game(game_rate) VALUES(" . implode(") , (", $_POST['game_rate']) . ")"
假设值为整数。如果它们是字符串,则必须正确添加引号
答案 1 :(得分:1)
尝试使用foreach循环。
$game = $_POST['game_rate']; // with the proviso $ _ POST ['game_rate'] is an array
$game_opt = $_POST['game_opt'];
$index = 0;
foreach($game as $g){
$query = "INSERT INTO t_game(game_rate, game_opt) VALUES('$g', '".$game_opt[$index]."')";
// Execute the query variabel
$index++;
}
答案 2 :(得分:0)
尝试使用以下代码
foreach($_POST['game_rate'] as $k=>$v){
if($k==0){
$temp = " values('$v','".$_POST['game_opt'][$k]."')";
}else{
$temp .= ", values('$v','".$_POST['game_opt'][$k]."')";
}
}
$sql = "INSERT INTO t_game(game_rate,game_opt) $temp";
//Execute your query here