Change image upload from mysql_query to PDO

Asked

Viewed 1,177 times

1

I would like to change this code that is working with mysql_* for PDO:

<?php
if ( isset($_POST['enviar']) ) {
    $arquivo = $_FILES['arquivo']['name'];
    $update = mysql_query("UPDATE cabecalho SET logo = '$logo");

    if ($update == '') {
        echo "<script language='javascript'>window.alert('Erro ao atualizar imagem!!!');</script>";
    } else {
        echo "<script language='javascript'>window.alert('Imagem cadastrada com sucesso!!!');</script>";

        //referenciar a classe upload
        include("../Upload.class.php");

        $imagem = "";

        if( isset($_FILES["arquivo"]) ){
            $dir_dest = "../upload";

            //linha que faz o upload
            $upload = new Upload($_FILES['arquivo'], $dir_dest);

            // verifica se foi realizado corretamente o upload
            if ($upload->processed) {
                $imagem=$upload->file_dst_name;
            }
        }

        echo "<img src='../upload/$imagem'/><br /><br />";
        echo "<img src='../upload/mini/$imagem'/>";
    }
}
?>
<form action="" method="post" enctype="multipart/form-data" name="cadastro">
    Imagem do Logo
    <br /><br />
    Logo : <input type="file" name="logo" />
    <br /><br />
    <input type="submit" value="Cadastrar" name="enviar">
</form>

I hug you all, and you get the attention.

  • very good your question, do the following place how far you have come and what test you have done so I can help you

  • I answered your question, but your logic is wrong. You did not pass the database structure, you also did not pass the contents of the Upload.class.php file. I will improve the answer if you give me this information.

1 answer

2

First it is necessary a connection with the PDO, follows below an example.

$host="localhost";
$login="root"; // Seu login do banco de dados
$senha=""; // Sua senha do banco de dados
$db="nome_banco"; // Nome de sua base de dados
$conn = new PDO("mysql:host=$host;dbname=$db",$login,$senha);

Once this is done it is necessary to prepare your queries, follows below the code after finished:

However you have problems in your logic, and will not work properly, plus the treatment to use PDO is the example below. You can study the implementation of Pdo instead of mysql_* I believe that understanding how it works would be better for you to do your development.

<?php

$host="localhost";
$login="root"; // Seu login do banco de dados
$senha=""; // Sua senha do banco de dados
$db="nome_banco"; // Nome de sua base de dados
$conn = new PDO("mysql:host=$host;dbname=$db",$login,$senha);

if(isset($_POST['enviar'])){
    $arquivo = $_FILES['arquivo']['name'];
    $update = $conn->prepare("UPDATE cabecalho SET logo = :logo"); //Preparo a string de conexão
    $update->bindParam(':logo', $logo); // Faço o bind dos parametros

    if(!$update->execute()){ //Executo a query
        echo "<script language='javascript'>window.alert('Erro ao atualizar imagem!!!');</script>";
    } else {
        echo "<script language='javascript'>window.alert('Imagem cadastrada com sucesso!!!');</script>";

        //referenciar a classe upload
        include("../Upload.class.php");
        $imagem = "";
        if(isset($_FILES["arquivo"])) {
            $dir_dest = "../upload";
            //linha que faz o upload
            $upload = new Upload($_FILES['arquivo'], $dir_dest);
            // verifica se foi realizado corretamente o upload
            if ($upload->processed) {
                $imagem=$upload->file_dst_name;
            }
        }
        echo "<img src='../upload/$imagem'/><br /><br />";
        echo "<img src='../upload/mini/$imagem'/>";
    }
    $update->closeCursor(); //Encerro a conexão.
}
?>
<form action="" method="post" enctype="multipart/form-data" name="cadastro">

Imagem do Logo<br /><br />
Logo : <input type="file" name="logo" /><br /><br />
<input type="submit" value="Cadastrar" name="enviar">
</form>
  • 3

    I did not understand the negative vote in this answer, it is useful and describes the procedures to do the update that the op requested with PDO.

  • 1

    I think the same Eduardo, but I believe that the question does deserve a negative vote because it escapes a little from the focus of the community. It should remove doubts of implementation of independent PDO for what is ('uploads, updates, inserts, queries...') because I think knowing this the rest is only logical in the implementation.

Browser other questions tagged

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