使用PHP将多个行从一个AJAX POST插入数据库?

时间:2015-11-03 15:05:49

标签: php mysql sql mysqli

帖子标题:

seasonString: Sommer,Hebst,Frühling,Winter
holidaywishString: 3,4,7,8                          

PHP:

//Insert Seasons data
$sqli="INSERT INTO seasons(season)VALUES('{$mysqli->real_escape_string($_POST['seasonString'])}')";
$insert=$mysqli->query($sql);
// $userID = mysqli_insert_id($mysqli);
//Insert Desires data
$sqli="INSERT INTO desires(desires)VALUES('{$mysqli->real_escape_string($_POST['holidaywishString'])}')";
$insert=$mysqli->query($sql);
// $userID = mysqli_insert_id($mysqli);

在这种情况下,数据将插入到我的数据库表tbl_season中的一列:

Sommer, Hebst, Frühling, Winter
holidaywishString:3, 4, 7, 8    

但我希望它是这样的:

tbl_season;
id    season
-1    Sommer                        
-1    Hebst
-2    winter
-3    hebst

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

$ arrSeason = explode(',',$ _POST ['seasonString']);     foreach($ arrSeason as $ season){         $ sqlSeason =“INSERT INTO season(season,iduserinfo)VALUES('”。$ mysqli-> real_escape_string($ season)。“','$ userID')”;         $ mysqli->查询($ sqlSeason);     }

/问题解决了很多问题