0

I've form to insert data use insert_batch CI 3, my plan is use insert_batch to insert more data in one time and javascript to create dynamically the form. Below the code I've to try it.

Controller

public function insert_data()
{
    // Processing uploaded images
    $config['upload_path'] = 'training_assets/image_training/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1024'; // Maximum size allowed in KB
    $this->load->library('upload', $config);

    // Get the text field values
    $id_article = $this->input->post("id_article");
    $date = $this->input->post('date', TRUE);

    // Initialize an array to hold the batch data
    $data = array();

    foreach ($_FILES['images']['name'] as $key => $image) {
        $_FILES['userfile']['name'] = $_FILES['images']['name'][$key];
        $_FILES['userfile']['type'] = $_FILES['images']['type'][$key];
        $_FILES['userfile']['tmp_name'] = $_FILES['images']['tmp_name'][$key];
        $_FILES['userfile']['error'] = $_FILES['images']['error'][$key];
        $_FILES['userfile']['size'] = $_FILES['images']['size'][$key];

        // Check if file upload is successful
        if ($this->upload->do_upload()) {
            $upload_data = $this->upload->data();

            // Prepare row data for insertion
            $row_data = array(
                'question' => $this->input->post('question', TRUE)[$key],
                'a' => $this->input->post('a', TRUE)[$key],
                'b' => $this->input->post('b', TRUE)[$key],
                'c' => $this->input->post('c', TRUE)[$key],
                'd' => $this->input->post('d', TRUE)[$key],
                'key' => $this->input->post('key', TRUE)[$key],
                'img' => $upload_data['file_name']
            );

            // Add non-array data to each row
            $row_data['id_article'] = $id_article;
            $row_data['date'] = $date;

            // Add row data to the batch
            $data[] = $row_data;
        } else {
            echo "File upload failed for image $key.";
        }
    }

    // Insert data to database
    if (!empty($data)) {
        var_dump($data);
        //$this->db->insert_batch('tb_question', $data);
    } else {
        echo "No data to insert.";
    }

    //redirect("question");
}

HTML

...<label>Question</label>
          <textarea type="text" class="form-control" name="question[]" placeholder="Enter question" />
<input readonly id="id_article" class="form-control" name="id_article" />
<div class="col-sm-2">
    <input readonly type="text" class="form-control" name="date" value="<?= date("Y/m/d"); ?>" />
  </div>...

I want to insert id_article and date but not insert into array, how to do it?, and is it possible?,

thank you

3
  • what is the output of var_dump($data); ? Commented Jun 10, 2024 at 11:08
  • got NULL for id_article and date Commented Jun 10, 2024 at 15:34
  • I think there was an issue issue on form post values are not receiving hate controller file, confirm $id_article = $this->input->post("id_article"); and $date = $this->input->post('date', TRUE); value comming or not Commented Jun 13, 2024 at 6:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.