Upload and save PHP image

-1

I have a problem when uploading images to the server, only the images in the table should be updated if a checkbox is checked, otherwise save the previous value

the database only saves the name of the associated image

Form.html

<form name ="dormUpdate" class="form" method="post" action="update.php" align="center">

    <div align="center"></div>
    <div class="info" align="center">
        <fieldset>
            <p> Producto </p>
            <label for="id"> <input id="id"   value="<?php echo $id; ?>"      type="hidden"   name="id" />
            <label for="imagen_chica"> <span>Imagen Chica:  </span>  <input id="imagen_chica"   value="<?php echo 'No Change'; ?>"   type="file"     name="imagen_chica"  /></label><br>
            <label for="changed1"> <span>Cambiar:  </span>  <input id="changed1"     type="checkbox"     name="changed1"  /></label><br>
            <label for="imagen_grande"> <span>Imagen Grande:</span>  <input id="imagen_grande"  value="<?php echo 'No Change'; ?>"   type="file"     name="imagen_grande" /></label><br>
            <label for="changed2"> <span>Cambiar:  </span>  <input id="changed2"     type="checkbox"     name="changed2"  /></label><br>
        </fieldset></form>

update.php

<?php
ini_set('display_errors',1);
require "funciones.php";
$ch1      = 0;
$ch2      = 0;
$img_ch   = 'NOT SET';
$img_gr   = 'NOT SET';

$con      = conecta();
$id       = $_REQUEST['id'];
$ch1      = $_REQUEST['changed1'];
$ch2      = $_REQUEST['changed2'];



$target_path = './fotos';
$img1path = $target_path .  $_FILES['imagen_chica']['name'];
if(move_uploaded_file($_FILES['imagen_chica']['tmp_name'], $img1path))
{
    $img_ch = $_FILES['imagen_chica']['name'];
 }

$img2path = $target_path . $_FILES['imagen_grande']['name'];
if(move_uploaded_file($_FILES['imagen_grande']['tmp_name'], "$img2path"))
{
    $img_gr = $_FILES['imagen_grande']['name'];
}


$res = mysqli_query($con,"SELECT * FROM productos WHERE id = $id;");
$row = mysqli_fetch_assoc($res) ;
$img_1orig    = $row['imagen_chica'];
$img_2orig    = $row['imagen_grande'];

if($ch1 != "on"){
    $img_gr = $img_1orig;
}
if($ch2 != "on"){
    $img_ch = $img_2orig;
}


$res = mysqli_query($con,
                    "UPDATE productos SET
                            imagen_chica  = '$img_ch',
                            imagen_grande = '$img_gr'
                            WHERE id      =  $id;"
);

/////////////////////////////

    
asked by Diego Vallejo 22.05.2017 в 00:39
source

1 answer

1

The problem is that you have forgotten the enctype="multipart/form-data" attribute. This value is necessary when using forms that have a file upload control

  

Note: The enctype attribute can only be used if we use method = "post" .

Example:

<form method="POST" enctype="multipart/form-data">

The second error is in your route, you must add at the end / .

Example:

$target_path = './fotos/';
    
answered by 22.05.2017 / 08:17
source