gpt4 book ai didi

php - 在数据库中存储数组会导致 PHP 通知 undefined offset 1

转载 作者:行者123 更新时间:2023-11-29 17:56:30 24 4
gpt4 key购买 nike

从 Android 客户端获取数组。将其存储在数据库中会出现 Notice: Undefined offset: 1 in C:\filepath\on line 67

我正在尝试在我的数据库中实现这一目标:

User_Id  | Book_Id---------|---------3        | 120---------|---------1        | 17---------|---------

php file that collects the post from android client:

if(isset($_POST['watch'])){
$jsonwatch = $_POST['watch'];
$array = json_decode($jsonwatch, true);
$response["error"] =! ($db_watch->storeClass($array['attendees'], $array['lesson_iDs']));

}
通知中调用的

php文件:

public function storeClass($attendees, $lesson_iDs) {
$stmt = $this->conn->prepare("INSERT INTO watch (date, user_id, lesson_id) VALUES(NOW(), ?, ?)");
$stmt->bind_param("ii", $attendees, $lesson_id);

foreach ($attendees as $index => $attendee) {
$lesson_id = $lesson_iDs[$index]; // Line 67
$result = $stmt->execute();
if(!$result){
echo '\n';
printf("Error for attendee %d in lesson %d : %s.\n", $attendee, $lesson_id, $stmt->error);
echo '\n';
echo '\n';
}
}

$stmt->close();

// result
return $result;
}

更新!

var_dump($array):

C:\wamp64\www\ghanaweb\AddClass.php:19:
array (size=4)
'attendees' =>
array (size=2)
0 => string '5a6611e89e9e26.27646060' (length=23)
1 => string '5a4cfae69fd7b6.01373362' (length=23)
'id' => int 0
'lesson_iDs' =>
array (size=1)
0 => int 4
'lesson_id' => int 0
{"error":false}

最佳答案

你的问题是你使用 $ 索引作为你的情况下的 ID,它只是 masive 的编号,仅此而已尝试使用

$lesson_id = $lesson_iDs[$attendee['lesson_id']];

关于php - 在数据库中存储数组会导致 PHP 通知 undefined offset 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48764939/

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