PHP存储数组进入单独的列

时间:2018-04-18 08:17:40

标签: php sql arrays mysqli

目前我有一个数组:

$movie_seat = "seat1, seat2, seat3, seat4";

foreach($movie_seat as $keys =>$values)
        {
        $movie_id = $values["movie_id"];
        $movie_seats = $values["movie_seats"];

            $query = "INSERT INTO `testing` (`movie_id`,`movie_seat`) VALUES ('$movie_id ','$movie_seats')" ;

            $addmoviequery = mysqli_query($conn,$query);
            if(!$addmoviequery){
                die("QUERY FAILED".mysqli_error($conn));

            }
从数组

,我将得到以下结果存储在database.It将整个数组存储到movie_seat列。

+-----------------+-------------------------------+
| movie_id        | movie_seat                    |
+-----------------+-------------------------------+
| movie01         | seat1, seat2, seat3           |
| movie02         | seat5,seat6                   |
+-----------------+-------------------------------+

但是,我想记录到不同列的数据库

我想要的结果:

 +-----------------+-------+-------+-------+-------+-------+-------+
 | movie_id        | seat1 | seat2 | seat3 | seat4 | seat5 | seat6 |
 +-----------------+-------+-------+-------+-------+-------+-------+
 | movie01         | booked|booked |booked |       |       |       |
 | movie02         |       |       |       |       |booked |booked |
 +-----------------+-------+-------+-------+-------+-------+-------+

我尝试使用爆炸功能,但会显示

  

警告:在C:\

中为foreach()提供的参数无效

1 个答案:

答案 0 :(得分:2)

您正在做的事情中存在多个错误。

$movie_seat = "seat1, seat2, seat3, seat4"; 

这不是array 。这是string

你必须explode将它放入一个数组中,如下所示:

$seats = explode(",", $movie_seat);

这将创建一个名为array的{​​{1}}字符串,其中$seats等于" seat1"等...

然后,您需要使用您创建的新数组$seats[0],而不是旧变量。像这样:

foreach