多个 MYSQLI 插入数组中的数据

Multiple MYSQLI insert with data from arrays

我需要帮助解决这个问题。我需要 运行 MYSQLI INSERT 几次并从数组中插入数据。

我从一个表单中获取数据,该表单具有两个动态值,一个日期跨度和一个来自最终用户输入的数字。我需要将其插入数据库中的 table 中。第二个数组中的数据需要在第一个数组中的所有日期插入。

第一个数组数组 ( [0] => 2020/01/26 [1] => 2020/01/27 [2] => 2020/01/28 [3] )

第二个数组数组 ( [0] => 1 [1] => 2 [2] => 3 [3] => )

这是我希望插入到我的数据库中的内容:

INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/26,1)
INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/26,2)
INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/26,3)

INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/27,1)
INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/27,2)
INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/27,3)

INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/28,1)
INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/28,2)
INSERT INTO MYTABLE ('bookingdate', 'squarenumber',) VALUES (2020/01/28,3)

谁能帮帮我?这对我来说有点难......

FORM 
<div id="massbooking-form-wrapper">
    <div class="massbooking-form-select-squares-wrapper">
        <h2>Välj rutor på område:<br> <?php echo getAreaname($link, $sq_areas_id); ?></h2>
        <br><br>
        <form name="booking_form" id="booking_form" action="<?php echo $bookingHomeUrl;?>/modules/bookings/process-massbooking.php" method="post">
            <div class="form-group" >
                <label>Startdatum<label>
                <input type="text" disabled name="booking_from_date_to_post"  class="form-control" value="<?php echo $booking_from_date?>">
            </div>
            <div class="form-group" >
                <label>Slutdatum<label>
                <input type="text" disabled  name="booking_to_date_to_post" class="form-control" value="<?php echo $booking_to_date?>">
            </div>
            <div class="form-group">
                <label>Rutor:</label>
                <input type="text" name="squarename_fill" id="squarename_fill" class="form-control" value="">   
            </div>

            <input type="text" hidden name="booking_from_date_to_post" class="form-control" value="<?php echo $booking_from_date?>">
            <input type="text" hidden name="booking_to_date_to_post" class="form-control" value="<?php echo $booking_to_date?>">
            <div class="form-group">
                <input type="submit" class="btn btn-success" onclick="return submitBookingConfirm()" value="Boka">
                <a class="btn btn-warning float-right" href="<?php echo $bookingHomeUrl;?>/welcome.php">Avbryt</a>
                <input type="reset" class="btn btn-default float-right" value="Rensa allt.">
            </div>
        </form>
    </div> 
</div>

    Getting all the dates in the span 

        function getDatesFromRange($start, $end, $format = 'Y/m/d') {
        /**
         * Generate an array of string dates between 2 dates
         */
            $array = array();
            $interval = new DateInterval('P1D');

            $realEnd = new DateTime($end);
            $realEnd->add($interval);

            $period = new DatePeriod(new DateTime($start), $interval, $realEnd);

            foreach($period as $date) { 
                $array[] = $date->format($format); 
            }
            return $array;
        }

    print_r (getDatesFromRange($booking_from_date_to_post, $booking_to_date_to_post));
    echo '<br>';echo '<br>';

//Converting squares from string to array
$splitSquarename_fill = explode(',', $squarename_fill);
print_r($splitSquarename_fill);

你还没有真正解释你有什么问题。您显示的 SQL 不是正确的语法,所以我假设您正在努力使用正确的 SQL 语法。 MySQLi 中的插入语句对此并不困难,您所要做的就是在两个数组上循环并为每个值执行该语句。

$stmt = $mysqli->prepare('INSERT INTO MYTABLE (bookingdate, squarenumber) VALUES(?,?)');
$stmt->bind_param('ss', $bookingdate, $squarenumber);
foreach ($array1 as $bookingdate) {
    foreach ($array2 as $squarenumber) {
        $stmt->execute();
    }
}