在DB中存储数组给PHP注意未定义的偏移量1

时间:2018-02-13 10:46:18

标签: php android mysql arrays

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

我想在我的数据库中实现这个目标:

User_Id  | Book_Id
---------|---------
3        | 120
---------|---------
1        | 17
---------|---------
android客户端收集帖子的

php文件:

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}

2 个答案:

答案 0 :(得分:0)

你的问题是你在你的情况下使用$ index作为ID而且它只是masive的数量,仅此而已 尝试使用

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

答案 1 :(得分:0)

$ lesson_iDs数组只有一个元素,在for循环中你试图访问第二个不存在的元素