简体   繁体   中英

PHP inserting only 10 rows to Mysql via a foreach loop

I am stuck while inserting data to my mysql table using a foreach loop to insert multiple rows at a time.

foreach($event as $booking){

    $startDate = $booking[DTSTART];
    $checkIn = strtotime($startDate[value]);

    $check_in = date("Y-m-d", $checkIn);

    $endDate = $booking[DTEND];
    $checkOut = strtotime($endDate[value]);

    $check_out = date("Y-m-d", $checkOut);

    $booking_source = "Airbnb";

    $source_id = "2";

    $summary = $booking[SUMMARY];

    $description = $booking[DESCRIPTION];

    $uid = $booking[UID];

    $sql = "INSERT INTO test_ics (uid, check_in, check_out, summary, description, source, source_id) VALUES (?,?,?,?,?,?,?)";

    $result = $conn->prepare($sql);
    $result->bind_param('sssssss', $uid, $check_in, $check_out, $summary, $description, $booking_source, $source_id);
    $result->execute();
    echo "<br>";
    echo "$check_in";
}

The code is working fine and inserting all the rows when I remove description and summary fields from the table where both of them are VARCHAR(2550) but when I am trying to run the code to insert rows with description and summary (like it is in the code above) its inserting only 10 rows.

Thanks

Notice: Use of undefined constant DTSTART - assumed 'DTSTART' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 40

Notice: Use of undefined constant value - assumed 'value' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 41

Notice: Use of undefined constant DTEND - assumed 'DTEND' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 45

Notice: Use of undefined constant value - assumed 'value' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 46

Notice: Use of undefined constant SUMMARY - assumed 'SUMMARY' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 54

Notice: Use of undefined constant DESCRIPTION - assumed 'DESCRIPTION' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 56

Notice: Use of undefined constant UID - assumed 'UID' in /home/fadipro/public_html/admin/get-airbnb-bookings.php on line 58

You can remove the notices by putting the string indexes in quotes like eg this $booking['DTSTART'] .

To catch the database errors, I assume you are using exceptions to report them. This code catches and prints the exception:

    try {
        $result = $conn->prepare($sql);
        $result->bind_param('sssssss', $uid, $check_in, $check_out, $summary, $description, $booking_source, $source_id);
        $result->execute();
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
    }

My guess is it will reveal data that is too long for the column.

If your fields name is not constant then you have to write it with single quotes

foreach($event as $booking){
  $startDate = $booking['DTSTART'];
  $checkIn = strtotime($startDate['value']);

  $check_in = date("Y-m-d", $checkIn);

  $endDate = $booking['DTEND'];
  $checkOut = strtotime($endDate['value']);

  $check_out = date("Y-m-d", $checkOut);

  $booking_source = "Airbnb";

  $source_id = "2";

  $summary = $booking['SUMMARY'];

  $description = $booking['DESCRIPTION'];

  $uid = $booking['UID'];

  $sql = "INSERT INTO test_ics (uid, check_in, check_out, summary, description, source, source_id) VALUES (?,?,?,?,?,?,?)";

  $result = $conn->prepare($sql);
  $result->bind_param('sssssss', $uid, $check_in, $check_out, $summary, $description, $booking_source, $source_id);
  $result->execute();
  echo "<br>";
  echo "$check_in";
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM