Update table without losing information

Asked

Viewed 88 times

0

I’m making a profile screen with photo. The upload and display of it is ok. But when I change some other page data and saved, the image is deleted from the bank (or at least some of the bank). How can I change data on this screen without losing the current profile image?

Screen view:

<?php foreach($info as $info): ?>

<div class="form-group">
        <img src="assets/images/perfil/<?php echo $info['foto']; ?>" border="1" class="perfil_pic" /><br />
        <input type="file" name="foto" class="btn btn-default add_file" style="float:left;"/>
    </div>

<?php endforeach; ?>

Controller da tela:

$u = new Usuarios();

if(isset($_POST['nome']) && !empty($_POST['nome'])){
      $img = $_FILES['foto'];
      $email = addslashes($_POST['email']);
      $senha = base64_encode($_POST['senha']);
      $nome = addslashes($_POST['nome']);
      $sobrenome = addslashes($_POST['sobrenome']);
            $aniversario = addslashes($_POST['aniversario']);
            $bio = addslashes($_POST['bio']);

      $u->updatePerfil($img, $email, $senha, $nome, $sobrenome, $aniversario, $bio);

Screen model:

public function updatePerfil($pic, $email, $senha, $nome, $sobrenome, $aniversario, $bio){
            $id = $_SESSION['fkr'];
            $url = '';
            if (count($pic) > 0) {
                $tipos = array('image/jpeg','image/jpg','image/png');
                if (in_array($pic['type'], $tipos)) {
                    $url = 'perfilatual';
                    switch($pic['type']){
                        case 'image/jpeg':
                        case 'image/jpg':
                            $url .= '.jpg';
                            break;
                        case 'image/png':
                            $url .= '.jpg';
                            break;
                }
            }
             move_uploaded_file($pic['tmp_name'], 'assets/images/perfil/' . $url);
         }
            $sql = "UPDATE usuarios SET foto = '$url', senha = '$senha', email = '$email', nome = '$nome', sobrenome = '$sobrenome', aniversario = '$aniversario', bio = '$bio' WHERE id = '$id'";
            $this->db->query($sql);
        }
  • Are you using any framerwork? !

  • Hello, thanks for the tip about the tag. The project is in MVC.

  • If it were me... in your case I wouldn’t save the image url in the database. Would simply upload the image by naming the image as the user ID.

  • You are probably not checking whether the image coming from the request is null and is replacing the existing one with a null.

  • And as I see it?

1 answer

0

Controller

public function adicionar() {
$data['nome'] = $this->input->post('nome');

if ($this->Usuarios_model->adicionar('usuarios', $data)) {
        $id_usuarios = $this->db->insert_id();

        move_uploaded_file($_FILES['userfile']['tmp_name'], 'upload/imagens_usuarios/' . $id_usuarios . '.jpg');            

        $this->session->set_flashdata('msg','Usuário Adicionado');
        redirect('admin/usuarios/index', 'refresh');
    }

}

public function editar()
{

$data['nome'] = $this->input->post('nome');
if ($this->Usuarios_model->editar('usuarios', $data, 'id', $this->input->post('id')) == TRUE) {

move_uploaded_file($_FILES['userfile']['tmp_name'], 'upload/imagens_usuarios/' . $this->input->post('id') . '.jpg');

$this->session->set_flashdata('msg','Usuário Editado');         
redirect('admin/usuarios/index', 'refresh');

}

}

Put this code on your model:

    //IMAGE URL//
function get_image_url($type = '', $id = '') {
    if (file_exists('upload/imagens_usuarios' . '/' . $id . '.jpg'))
        $image_url = base_url() . 'upload/imagens_usuarios' . '/' . $id .  '.jpg';
    else
        $image_url = base_url() . 'upload/imagens_usuarios/padrao.jpg';

    return $image_url;
}

Use this code to load the image in the View

<input class="file-input" type="file" onchange="preview(this)" name="userfile" style="opacity: 0"/>
<img id="" class="" src="<?php echo $this->Usuarios_model->get_image_url('usuarios');?>" alt="">
  • Hello, it worked for me. The upload apparently happens but does not update.

  • *It didn’t work... I meant

  • What do you mean, the upload apparently happens? ?

  • Sorry, I meant there was no bug or error, but the upload doesn’t happen. Remembering that I’m still Noob in PHP... rsrss

  • The bizarre thing is that the other information in this profile (name, surname, etc.), even if not changed, remains normal. Only the image that if it is not uploaded again disappears.

  • Post here, the function you are using to add image and upload, please!

  • Add it up, because as Danilo said, You are not checking whether the image that comes from the request is null and is replacing the existing one with a null one. For the image you should have something like if (!empty($_FILES['userfile']['name'])):

  • How do I do it? <code></code>

  • Do so? <code>if (isset($_FILES['photo']['name']) && !Empty($_FILES['photo']['name'])) { $img = $_FILES['photo']; }</code>

  • So I put if (isset($_FILES['photo']) && !Empty($_FILES['photo'])) { $img = $_FILES['photo']; } and upload is ok, but the update problem remains. What should go on Else?

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.