Image in Database with Input File

Asked

Viewed 818 times

0

Hello folks I am trying to put images in the database through FILE input, for image/port/mini and image/port/full paths.

Form:

<form action="recebe.php" method="POST">
         <label>NOME DO CLIENTE:</label>
            <input type="text" name="nome" style="
    margin-left: 50px;
"><br><br />
         <label>TIPO DE SERVIÇO</label>
            <input type="text" name="tipo" style="
    margin-left: 57px;
"><br><br />
         <label>MENU:</label>
        <fieldset>
        <input type="checkbox" name="menu[]" id="visu" value="visu"><label for="visu">IDENTIDADE VISUAL</label><br>
        <input type="checkbox" name="menu[]" id="web" value="web"><label for="web">DESENVOLVIMENTO WEB</label><br>
        <input type="checkbox" name="menu[]" id="grafico , tdmg" value="grafico"><label for="grafico,tdmg,">MATERIAL GRAFICO</label><br>

             <input type="checkbox" name="menu[]" id="visimg" value="visimg" class="radiosaq"><label for="visimg,">CARTÃO DE VISITA</label><br>
             <input type="checkbox" name="menu[]" id="pap" value="pap" class="radiosaq"><label for="pap,">PAPELARIA</label><br>
             <input type="checkbox" name="menu[]" id="fol" value="fol" class="radiosaq"><label for="fol,">FOLDER</label><br>
             <input type="checkbox" name="menu[]" id="card" value="card" class="radiosaq"><label for="card,">CARDÁPIO</label><br>
             <input type="checkbox" name="menu[]" id="rev" value="rev" class="radiosaq"><label for="rev,">REVISTA</label><br>
             <input type="checkbox" name="menu[]" id="emb" value="emb" class="radiosaq"><label for="emb,">EMBALAGEM</label><br>


        <input type="checkbox" name="menu[]" id="comu , tdcv" value="comu"><label for="comu,tdcv,">COMUNICAÇÃO VISUAL</label><br>

             <input type="checkbox" name="menu[]" id="amb" value="amb" class="radiosaq"><label for="amb,">AMBIENTAÇÃO</label><br>
             <input type="checkbox" name="menu[]" id="dec" value="dec" class="radiosaq"><label for="dec,">DECORAÇÃO</label><br>
             <input type="checkbox" name="menu[]" id="ade" value="ade" class="radiosaq"><label for="ade,">ADESIVOS</label><br>
             <input type="checkbox" name="menu[]" id="pla" value="pla" class="radiosaq"><label for="pla,">PLACAS</label><br>
             <input type="checkbox" name="menu[]" id="ban" value="ban" class="radiosaq"><label for="ban,">BANNERS</label><br>
             <input type="checkbox" name="menu[]" id="plo" value="plo" class="radiosaq"><label for="plo,">PLOTAGEM</label><br>
             <input type="checkbox" name="menu[]" id="out" value="out" class="radiosaq"><label for="out,">OUTROS</label><br>


        <input type="checkbox" name="menu[]" id="digi" value="digi"><label for="digi,">MARKETING DIGITAL</label><br>
  </fieldset><br /><br />
         <label>IMAGEM MINI:</label>  <input type="file" name="imageM" accept="image/port/mini"><br><br />
         <label>IMAGEM GRANDE:</label>  <input type="file" name="imageF" accept="image/port/full"><br><br />
       <label>DESCRIÇÃO</label><br />
           <textarea id="desc" name="desc" required=""></textarea><br><br />
     <input type="submit" value="ENVIAR DADOS PARA O SITE :D">
   </form>

part of the images:

<label>IMAGEM MINI:</label>  <input type="file" name="imageM" accept="image/port/mini"><br><br />
             <label>IMAGEM GRANDE:</label>  <input type="file" name="imageF" accept="image/port/full"><br><br />

CONNECTION:

  <?php
$servidor = 'localhost';
$banco      = 'apixel_galeria';
$usuario  = 'root';
$senha    = '';
$conn     = @mysql_connect($servidor, $usuario, $senha) or die (mysql_error());
$db          = mysql_select_db($banco,$conn) or die (mysql_error());

$charset = mysql_set_charset("utf8");

?>

php.

<?php

require_once("conn.php");

$nome=$_POST['nome'];
$tipo=$_POST['tipo'];
$desc=$_POST['desc'];
$menu = isset( $_POST['menu'] ) && is_array( $_POST['menu'] )
        ? implode( ' , ', $_POST['menu'] ) : '';

$imgm=$_POST['imageM'];
$imgf=$_POST['imageF'];

$query = "INSERT INTO `portfolio` (`nome`, `tipo`, `desc`, `menu`, `imageM`, `imageF`) VALUES ('".$nome."', '".$tipo."', '".$desc."', '".$menu."', '".$imgm."', '".$imgf."')";

// Executa a query
$inserir = mysql_query($query);

if ($inserir) {
echo "Post inserido com sucesso!";
} else {
echo "Não foi possível inserir o Post, tente novamente.";
// Exibe dados sobre o erro:
echo "Dados sobre o erro:" . mysql_error();
}
  • At first glance you forgot to put enctype="Multipart/form-data" attribute inside the form tag... But what exactly is the error/problem?

  • @sullyvan.Nunes keeps showing up these two Undefined index errors: image and Undefined index: imageF inside the receive.php

  • ae just do not send form these 2 image and imageF the rest sends normally

  • error indicates that $_POST['imageF'] and $_POST['image'] do not exist (are not being sent), try placing the enctype attribute with the multiplart/form-data value inside the form tag and see if the error remains

  • yes this giving error with it. put in the form <form action="receive.php" method="POST" enctype="Multipart/form-data">

  • forgot to comment. don’t use $_FILES[''] instead of $_POST[''']... a read in the first http://php.net/manual/en/reserved.variables.files.php because $_FILES['] is an array.

  • these topics I bugle enough for being amateur rsrrs

Show 2 more comments

1 answer

1


Hello, first, because it is a form that will send files to the server, it is necessary that the form has the attribute enctype -> enctype="multipart/form-data".

for being a file of type "file", this code snippet is wrong.

$imgm=$_POST['imageM'];
$imgf=$_POST['imageF'];

to receive the file name, it is possible to use this code snippet:

$imgm=$_FILES['imageM']['name'];
$imgf=$_FILES['imageF']['name'];

to save the file you can use the php function move_uploaded_file($file, $target), passing as first parameter the temporary location of the file, an example of this function:

$target = "minhapasta/arquivo/". $_FILES['imageM']['name'];
move_uploaded_file($_FILES['imagemM']['tmp_name'], $target);
  • Thank you @Guilherme Lopes

Browser other questions tagged

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