gpt4 book ai didi

PHP 通过 foreach 循环仅向 Mysql 插入 10 行

转载 作者:行者123 更新时间:2023-11-29 06:46:25 25 4
gpt4 key购买 nike

我在使用 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";
}

代码工作正常,当我删除 description 时插入所有行和 summary表中的字段,其中两个字段都是 VARCHAR(2550)但是当我尝试运行代码来插入带有 description 的行时和 summary (就像上面的代码一样)它只插入 10 行。

谢谢

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

最佳答案

您可以通过将字符串索引放在引号中来删除通知,例如这个$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();
}

我的猜测是它会显示对于该列来说太长的数据。

关于PHP 通过 foreach 循环仅向 Mysql 插入 10 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49234879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com