How to update an image name in the database?

Asked

Viewed 35 times

-1

This my script is to insert the name of the image in the bank, is working normally

INSERT.PHP

  if ($_POST){

    $imagem = $_FILES['img_post'];
    $nomeImg = $imagem['name'];
    $tmpImg = $imagem['tmp_name'];
    $formatoBase = explode('.',$nomeImg);
    $formato = end($formatoBase);           //jesus
    $nomeImg = "paranoid_img_".uniqid().".".$formato;

        $titulo = $_POST['titulo_post'];
        $descricao = $_POST['descricao_post'];
        $texto = $_POST['texto_post'];
        $autor = $_SESSION['nome']." ".$_SESSION['sobrenome'];
        $url = uniqid();
        $pastaImg = "../img/posts";

        $query = mysqli_query($conexao, "insert into tbl_post (titulo_post, descricao_post, img_post, texto_post, autor_post, idautor_post, data_post, url_post)
                                        values ('$titulo','$descricao','$nomeImg','$texto','$autor','$id_user','$data','$url')");

        if ($query){

            $uploadPasta = move_uploaded_file($tmpImg, $pastaImg."/".$nomeImg);
            echo "<script>alert('Post enviado!');location.href=('../painel_adm.php');</script>";

        } else {

            echo "<script>alert('Erro!');location.href=('../upload_post.php');</script>";

        }
}

Now I cannot perform the name update if I upload another image by the change form.

CHANGE.PHP

$query1 = mysqli_query($conexao,"select * from tbl_post where url_post = '$url'");
$contSql=mysqli_fetch_array($query1);

if ($_POST){

    $id = $contSql['id_post'];
    $titulo = $_POST['titulo_post'];
    $descricao = $_POST['descricao_post'];
    $texto = $_POST['texto_post'];
  $imagem = $_FILES['img_post'];
    $nomeImg = $imagem['name'];
    $tmpImg = $imagem['tmp_name'];




    $query2 = mysqli_query($conexao,"update tbl_post set titulo_post='$titulo', descricao_post='$descricao', texto_post = '$texto' img_post='$nomeImg' where id_post = $id");

    if ($query2){
        echo "<script>alert('Post atualizado!');location.href='show_post.php?url=$url';</script>";
    } else {
        echo "<script>alert('Erro!');</script>";
    }
}
?>
  • Show any errors? Have you checked if all the data is getting into PHP correctly? Tested SQL elsewhere to make sure it’s working? It seems to me that the problem is on this line $id = $contSql['id_post'];, id value is correct?

  • Yes William, if I remove the image update, the information goes up normal, if I do, just pop up the error Alert I put on Else if the $query was not executed correctly

1 answer

1

I got guys, follow the code for anyone who needs help

	<?php
    if ($_POST){

    $id = $contSql['id_post'];
    $titulo = $_POST['titulo_post'];
    $descricao = $_POST['descricao_post'];
    $texto = $_POST['texto_post'];
    if(isset($_FILES['img_post']['name']) && ($_FILES['img_post']     ['name']!="")) {
    $temp = $_FILES['img_post']['tmp_name'];
    $nomeImg = $_FILES['img_post']['name'];
    unlink("img/posts/$old_image");
    move_uploaded_file($temp, "img/posts/$nomeImg");
    }
    else {
      $nomeImg = $old_image;
    }

      $query2 = mysqli_query($conexao,"update tbl_post set               titulo_post='$titulo', descricao_post='$descricao',               img_post='$nomeImg', texto_post = '$texto' where id_post = $id");

    if ($query2){

      echo "<script>alert('Post atualizado!');location.href='show_post.php?url=$url';</script>";
    } else {
      echo "<script>alert('Erro!');</script>";
    }
   }
 ?>

  • Updating the image name in the database does not require moving a file! The question is not clear enough and the answer is identical. Maybe he meant update in the bank by the name of the new moved image. Another thing, who is $old_image

  • It does require a... I’m not only updating the image name, I’m replacing it with another one, so I have to remove the old image from the folder and move the new one, I think if I said I wanted to change the image in the bank it would be clearer. Ah, $old_image, I forgot to quote here because I set in head, I assign to this variable, the image I pull from the bank. $old_img=$row['img_post'];. Abraços Leo.

Browser other questions tagged

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