簡體   English   中英

PHP通過foreach循環僅向MySQL插入10行

[英]PHP inserting only 10 rows to Mysql via a foreach loop

我在使用foreach循環一次將多行插入數據到mysql表中時陷入困境。

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";
}

當我從兩個都為VARCHAR(2550)的表中刪除descriptionsummary字段時,代碼工作正常並插入了所有行,但是當我嘗試運行代碼以插入具有descriptionsummary行時(例如上面的代碼)僅插入10行。

謝謝

注意:使用未定義的常量DTSTART-在第40行的/home/fadipro/public_html/admin/get-airbnb-bookings.php中假定為'DTSTART'

注意:使用未定義的常數-在第41行的/home/fadipro/public_html/admin/get-airbnb-bookings.php中假定為'value'

注意:使用未定義的常量DTEND-在第45行的/home/fadipro/public_html/admin/get-airbnb-bookings.php中假定為'DTEND'

注意:使用未定義的常數-在第46行的/home/fadipro/public_html/admin/get-airbnb-bookings.php中假定為'value'

注意:使用未定義的常量摘要-在第54行的/home/fadipro/public_html/admin/get-airbnb-bookings.php中假定為“ SUMMARY”

注意:使用未定義的常量description-在第56行的/home/fadipro/public_html/admin/get-airbnb-bookings.php中假定為'DESCRIPTION'

注意:使用未定義的常量UID-在第58行的/home/fadipro/public_html/admin/get-airbnb-bookings.php中假定為'UID'

您可以通過將字符串索引放在諸如$booking['DTSTART']類的引號中來刪除通知。

為了捕獲數據庫錯誤,我假設您使用異常來報告它們。 此代碼捕獲並打印異常:

    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();
    }

我的猜測是它將顯示對於列而言太長的數據。

如果您的字段名稱不是常量,則必須用單引號將其寫入

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";
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM