CodeIgniter Forums
ERROR: Failed to get field data from Database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: ERROR: Failed to get field data from Database (/showthread.php?tid=92805)



ERROR: Failed to get field data from Database - kyanoe - 04-26-2025

I'm creating a surveys manager feature for my website. It can create a survey, edit, and delete. It's fine for the create, but not for the edit. When I'm trying to edit the "pertanyaan" (meaning: question) and added new questions that's not exist in the database, it throws an error like this:
ERROR: Failed to get field data from database.

That's somehow, it's caused by the update batch from the edit function I created.

For more information, the things that I've inspect are:
1. The columns whether on the website or the database are already good.
2. The HTML for loading the data is also good.
3. The data sent is also correct.

Here's the full code of edit function:
PHP Code:
function editPertanyaanData($database$data)
{
  $builder $database->table('s_pertanyaan');
  $idSurvey $data['id_survey'] ?: null;
  $idPertanyaan $data['id_pertanyaan'] ?: null;
  $pertanyaan $data['pertanyaan'] ?: null;
  $jenis $data['jenis'] ?: null;
  if (!$idSurvey || !$idPertanyaan || !$pertanyaan || !$jenis) {
    return null;
  }
  if (!empty($idPertanyaan)) {
    $builder->where('id_survey'$idSurvey)
      ->whereNotIn('id'$idPertanyaan)
      ->delete();
  }
  $pertanyaanDataUpdate = [];
  $pertanyaanDataInsert = [];
  foreach ($pertanyaan as $index => $teks) {
    $jenisValue = isset($jenis[$index]) && is_numeric($jenis[$index]) ? (int) $jenis[$index] : 1;
    if (empty($idPertanyaan[$index])) {
      $pertanyaanDataInsert[] = [
        'id_survey' => $idSurvey,
        'teks' => $teks,
        'jenis' => $jenisValue,
        'is_aktif' => true,
        'urutan' => $index 1,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      continue;
    }
    $pertanyaanDataUpdate[] = [
      'id' => $idPertanyaan[$index],
      'id_survey' => $idSurvey,
      'teks' => $teks,
      'jenis' => $jenisValue,
      'is_aktif' => true,
      'urutan' => $index 1,
      'updated_at' => date('Y-m-d H:i:s'),
    ];
  }
  if (!empty($pertanyaanDataUpdate)) {
    $builder->updateBatch($pertanyaanDataUpdate, ['id']);
  }
  if (!empty($pertanyaanDataInsert)) {
    $builder->insertBatch($pertanyaanDataInsert);
  }
  return $database->affectedRows() > 0;



And here's how I call it:
PHP Code:
public function editSurvey($idSurvey)
  {
    if ($this->request->getMethod() === "POST") {
      $data $this->request->getPost();
      $data['dokumen_pendukung_survey'] = $this->request->getFile('dokumen_pendukung_survey');
      $database Database::connect();
      $database->transStart();
      $data['id_survey'] = $idSurvey;
      editSurveydata($database's_survey', [
        'kode' => $data['kode_survey'],
        'nama' => $data['nama_survey'],
        'dokumen_pendukung' => $data['dokumen_pendukung_survey'],
        'status' => $data['status_survey'],
      ], $idSurvey);

      if (!$idSurvey) {
        $database->transRollback();
        $database->close();
        return;
      }
      $result editSurveydata($database's_pelaksanaan_survey', [
        'id_periode' => $data['id_periode'],
        'tanggal_mulai' => $data['tanggal_mulai'],
        'tanggal_selesai' => $data['tanggal_selesai'],
        'deskripsi' => $data['deskripsi_survey'],
        'created_at' => date('Y-m-d H:i:s'),
      ], $idSurvey);
      if (!$result) {
        $database->transRollback();
        $database->close();
        return;
      }
      $result editPertanyaanData($database$data);
      if (!$result) {
        $database->transRollback();
        $database->close();
        return;
      }
      $database->transCommit();
      $database->close();
      return alert('survey/manajemen-survey''success''Survey berhasil diupdate!');
    }

    $data['survey'] = $this->surveyModel->find($idSurvey);
    if (!$data['survey']) {
      return alert('survey/manajemen-survey''error''Survey tidak ditemukan!');
    }
    $data['pelaksanaan_survey'] = $this->pelaksanaanSurveyModel->where('id'$idSurvey)->first();
    if (!$data['pelaksanaan_survey']) {
      return alert('survey/manajemen-survey''error''Pelaksanaan survey tidak ditemukan!');
    }
    $data['periode'] = $this->periodeModel->findAll();
    $data['pertanyaan'] = $this->pertanyaanSurveyModel->where('id_survey'$idSurvey)->orderBy('urutan''asc')->findAll();
    echo view('layouts/header.php', ["title" => "Manajemen Survey"]);
    echo view('survey_kepuasan/manajemen_survey/edit_survey.php'$data);
    echo view('layouts/footer.php');