3

I made a user registration form on the CodeIgniter framework so users can registrate to my website. Now the only thing that doesn't work is that I cant upload a profile picture. When I click on the register button I'm getting 2 errors. I want the profile picture to be uploaded in the product_foto column.

This is my view file: (register.php) :

<form action="" method="POST" enctype='multipart/form-data'>



            <div class="form-group">
                <label for="voornaam" >Voornaam</label>
                <input class="form-control" name="voornaam" id="voornaam" type="text">
            </div>


            <div class="form-group">
                <label for="achternaam">Achternaam</label>
                <input class="form-control" name="achternaam" id="achternaam" type="text">
            </div>

            <div class="form-group">
                <label for="achternaam">Straat en huisnummer</label>
                <input class="form-control" name="straat" id="straat" type="text">
            </div>

            <div class="form-group">
                <input class="form-control" name="huisnummer" id="huisnummer" type="text">
            </div>





            <div class="form-group">
                <label for="huisnummer">Huisnummer</label>
                <input class="form-control" name="huisnummer" id="huisnummer">
            </div>


            <div class="form-group">
                <label for="postcode" >Postcode</label>
                <input class="form-control" name="postcode" id="postcode">
             </div>


            <div class="form-group">
                <label for="woonplaats" >Woonplaats</label>
                <input class="form-control" name="woonplaats" id="woonplaats">
            </div>

            <div class="form-group">
                <label for="email" >Email adres</label>
                <input class="form-control" name="email" id="email" type="emai">
            </div>

            <div class="form-group">
                <label for="wachtwoord" >Wachtwoord</label>
                <input class="form-control" name="wachtwoord" id="wachtwoord" type="password">
            </div>


                <div class="form-group">
                <label for="wachtwoord">Herhaal wachtwoord</label>
                <input class="form-control" name="wachtwoord2" id="wachtwoord" type="password">
            </div>

           <div class="form-group">
                <label for="profiel_foto">Profiel foto</label>
                <input class="form-control" type="file" name="profiel_foto" id="profiel_foto">
            </div> 

            <div class="form-group">
                <label for="beschrijving">Beschrijving</label>
                <input class="form-control" name="beschrijving" id="beschrijving">
            </div>


            <div class="form-group">
                <label for="geboortedatum" >Geboortedatum</label>
                <input class="form-control" name="geboortedatum" id="geboortedatum" type="date">
            </div>


            <div class="form-group">
                <label for="geslacht" >Geslacht</label>
                <select class="form-control" id="geslacht" name="geslacht">
                    <option value="Man">Man</option>   
                    <option value="Vrouw">Vrouw</option>   
                </select>
            </div>

            <div>
                <button class="btn btn-primary" name="register" >Registreren</button>
            </div>
            </form>

This is the register code in the controller:

public function register()
    {




         $config['upload_path'] = './upload/';
         $config['allowed_types'] = 'gif|jpg|png|jpeg';
         $this->load->library('upload', $config);
         $this->input->post('profiel_foto');
         $data_upload_files = $this->upload->data();

         $image = $data_upload_files['./upload/'];


                //voeg gebruiker toe aan database
                $data = array (
                    'voornaam'=>$_POST['voornaam'],
                    'achternaam'=>$_POST['achternaam'],
                    'email'=>$_POST['email'],
                    'wachtwoord'=>  ($_POST['wachtwoord']),
                    'startdatum'=>date('Y-m-d'),
                    'postcode'=>$_POST['postcode'],
                    'huisnummer'=>$_POST['huisnummer'],
                    'woonplaats'=>$_POST['woonplaats'],
                    'beschrijving'=>$_POST['beschrijving'],
                    'geboortedatum'=>$_POST['geboortedatum'],
                    'geslacht'=>$_POST['geslacht'],
                    'profiel_foto'=>$image
                    );
                $this->db->insert('users',$data);

                $this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
                redirect("auth/register", "refresh");
            }
        }

And these are the 2 errors I'm getting when I'm trying to registrate:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: ./upload/

Filename: controllers/Auth.php

Line Number: 131

Backtrace:

File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 131
Function: _error_handler

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once




A Database Error Occurred

Error Number: 1048

Column 'profiel_foto' cannot be null

INSERT INTO `users` (`voornaam`, `achternaam`, `email`, `wachtwoord`, `startdatum`, `postcode`, `huisnummer`, `woonplaats`, `beschrijving`, `geboortedatum`, `geslacht`, `profiel_foto`) VALUES ('hallo', 'hallo', 'hallo@gmail.com', 'hallo', '2017-06-28', 'hallo', 'hallo', 'hallo', 'hallo', '2017-06-10', 'Man', NULL)

Filename: controllers/Auth.php

Line Number: 149
lablanco
  • 103
  • 1
  • 9

2 Answers2

2

I modified your code. Try this

public function register() {
  $data = array();

  $config = array(
      'upload_path' => 'upload',
      'allowed_types' => 'gif|jpg|png|jpeg',
  );
  $this->load->library('upload', $config);

  if (!$this->upload->do_upload('profiel_foto')) {
    $error = array('error' => $this->upload->display_errors());
    // var_dump( $error); die; check errors 
  } else {
    $fileName = $this->upload->data();
    $data['profiel_foto'] = $fileName['file_name'];
  }
  // voeg gebruiker toe aan database
  $data = array (
      'voornaam'=>$_POST['voornaam'],
      'achternaam'=>$_POST['achternaam'],
      'email'=>$_POST['email'],
      'wachtwoord'=>  ($_POST['wachtwoord']),
      'startdatum'=>date('Y-m-d'),
      'postcode'=>$_POST['postcode'],
      'huisnummer'=>$_POST['huisnummer'],
      'woonplaats'=>$_POST['woonplaats'],
      'beschrijving'=>$_POST['beschrijving'],
      'geboortedatum'=>$_POST['geboortedatum'],
      'geslacht'=>$_POST['geslacht'],
  );
  $this->db->insert('users', $data);

  $this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
  redirect("auth/register", "refresh");
}
Muhammad Usman
  • 1,389
  • 12
  • 23
1

replace

$config['upload_path'] = './upload/';
         $config['allowed_types'] = 'gif|jpg|png|jpeg';
         $this->load->library('upload', $config);
         $this->input->post('profiel_foto');
         $data_upload_files = $this->upload->data();

         $image = $data_upload_files['./upload/'];

With

                    $target_dir = "upload/";
                    $target_file = $target_dir . time().basename($_FILES["profiel_foto"]["name"]);
                    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
                    $imgName = time().basename($_FILES["profiel_foto"]["name"]);
                    move_uploaded_file($_FILES["profiel_foto"]["tmp_name"], $target_file);

Your insert function

$data = array (
                    'voornaam'=>$_POST['voornaam'],
                    'achternaam'=>$_POST['achternaam'],
                    'email'=>$_POST['email'],
                    'wachtwoord'=>  ($_POST['wachtwoord']),
                    'startdatum'=>date('Y-m-d'),
                    'postcode'=>$_POST['postcode'],
                    'huisnummer'=>$_POST['huisnummer'],
                    'woonplaats'=>$_POST['woonplaats'],
                    'beschrijving'=>$_POST['beschrijving'],
                    'geboortedatum'=>$_POST['geboortedatum'],
                    'geslacht'=>$_POST['geslacht'],
                    'profiel_foto'=>$imgName
                    );
                $this->db->insert('users',$data);
Yadhu Babu
  • 1,425
  • 2
  • 8
  • 22
  • Not working I get this error now: A PHP Error was encountered Severity: Notice Message: Undefined variable: image Filename: controllers/Auth.php Line Number: 146 Backtrace: File: /home/ubuntu/workspace/application/controllers/Auth.php Line: 146 Function: _error_handler File: /home/ubuntu/workspace/index.php Line: 315 Function: require_once – lablanco Jun 29 '17 at 09:20
  • Its still not working man im getting this error: A PHP Error was encountered Severity: Notice Message: Undefined variable: imageName Filename: controllers/Auth.php Line Number: 129 Backtrace: File: /home/ubuntu/workspace/application/controllers/Auth.php Line: 129 Function: _error_handler File: /home/ubuntu/workspace/index.php Line: 315 Function: require_once – lablanco Jun 29 '17 at 11:22
  • replace `$imageName` with `$imgName`. – Yadhu Babu Jun 29 '17 at 11:32