How can I update a mysql blob field using php

Asked

Viewed 681 times

1

if(isset($_POST['submit_edit'])) {
         if (empty($_POST['imagem'])){
                                    $titulo = $_POST['titulo'];
                                    $texto = $_POST['texto'];
                                    $query ="UPDATE SERVICOS SET TITULO=\"$titulo\",TEXTO=\"$texto\" WHERE ID_SERVICO=$id";
                                    mysql_query($query);
                                    header("Location:index.php?ver=servicos.php");
                        }else {


                        $titulo = $_POST['titulo'];
                        $texto = $_POST['texto'];
                        $pFoto = $_FILES["imagem"]["tmp_name"];   
                        $pont = fopen($pFoto, "r"); 
                        $pTipo = $_FILES['imagem']["type"]; 
                        $mysqlImg = addslashes(fread($pont, filesize($pFoto))); 
                        $query = "UPDATE SERVICOS SET TITULO=\"$titulo\",TEXTO=\"$texto\",IMAGEM=\"$mysqlImg\",TIPO=\"$pTipo\" WHERE ID_SERVICO=$id";
                        mysql_query($query);
                        header("Location:index.php?ver=servicos.php");
                    }

                    }

I just want to update if the image upload field is filled

1 answer

0


I don’t particularly like to repeat code, so when I can, I condition only the affected part:

if(isset($_POST['submit_edit'])) {

    $titulo = $_POST['titulo'];
    $texto = $_POST['texto'];

    $query = "UPDATE SERVICOS SET TITULO=\"$titulo\",TEXTO=\"$texto\"";

    if (is_uploaded_file($_FILES["imagem"]["tmp_name"])) {
        $pFoto = $_FILES["imagem"]["tmp_name"];   
        $pont = fopen($pFoto, "r"); 
        $pTipo = $_FILES['imagem']["type"]; 
        $mysqlImg = addslashes(fread($pont, filesize($pFoto))); 
        $query .= ",IMAGEM=\"$mysqlImg\",TIPO=\"$pTipo\"";
    }

    $query .= " WHERE ID_SERVICO=$id";

    mysql_query($query);
    header("Location:index.php?ver=servicos.php");

}

Browser other questions tagged

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