0

Is it possible to get data from file then add it in my database in mysql. I'm using codeigniter. My code only asks inputs from the user. Please help please. It is required by my professor so that if the user wants to input for example 100 data he/she doesn't manually input the data thus get the data from another database.

my view page

        <!DOCTYPE html>

              <html lang="en">
                <head>
                   <meta charset="utf-8">
                <title>Students</title>


</head>

<body>
                   <div id="container">
                      <h1>Student's Page</h1>
                         <div id="body">   
                     <div id="div2">


                <?php

                echo form_open('main/addToRegistrar');
                ?>



                <br>





                <?php


                echo "<p>ID: ";
                echo form_input('id');
                echo "</p>";

                echo "<p>First Name: ";
                echo form_input('first_name');
                echo "</p>";

                echo "<p>Middle Name: ";
                echo form_input('middle_name');
                echo "</p>";

                echo "<p>Last Name: ";
                echo form_input('last_name');
                echo "</p>";





                echo "<p>";
                echo form_submit('ADD', 'ADD');
                echo "<p>"; 


                echo form_close();



                   ?>
                        <a href='<?php echo base_url()."main/admin"; ?>'>Home!</a>
                       </div>




                      </div>
                <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds </p>
                    </div>
           </body>
         </html>

my controller

       public function addToRegistrar(){
            if($this->session->userdata('is_logged_in')){
        $this->load->model('model_users');
        $this->model_users->addToRegistrar();
        $this->load->view('view_addAteneoStudents');
    } else{
        redirect('main/restricted');
    }
      }

my model

       public function addToRegistrar(){
                  $fname = $this->input->post('first_name');
                  $mname = $this->input->post('middle_name');
                  $lname = $this->input->post('last_name');
                  $id = $this->input->post('id');

                    $data = array(
                         'id' =>  $id,
                         'first_name' => $fname ,
                         'middle_name' => $mname ,
                         'last_name' => $lname
                                 );

                        if($this->db->insert('registrar', $data)){
                        echo "Successfully added";
                      } 
                        }

1 Answer 1

1

You must have an option to upload files (csv recommended) and upload it to your server. Here is a quick how:

view:

<html>
<body>

<form action="<?php echo base_url();?>" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

controller:

public function addToRegistrar()
{
    if($this->session->userdata('is_logged_in')){
        $this->load->model('model_users');
        $this->model_users->addToRegistrar();
        $this->load->view('view_addAteneoStudents');
    } else{
        redirect('main/restricted');
    }
}

model:

function addToRegistrar()
{
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      $filename = "upload/" . $_FILES["file"]["name"];
      move_uploaded_file($_FILES["file"]["tmp_name"], $filename);

      //parse the uploaded file and insert to database
      $header = NULL;
      $data = array();
      if (($handle = fopen($filename, 'r')) !== FALSE)
      {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = $row;
        }
        $this->db->insert_batch('registrar', $data);
        fclose($handle);
      }
    }
  }
}

NOTE: Your csv file should have the same column count as your database table 'registrar'.

More about reading csv files here.

Sign up to request clarification or add additional context in comments.

Comments

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.