UPDATE variables and data in PHP and MySQL

0

I need to save a path of an image in my code, in case the user does not want to modify the image when modifying a product.

The current problem is that it sends blank and modifies the url of the image in the database

<?php
	session_start();
	include '../head.php';
	$pro = $_GET['id_pro'];
	$sqli = "SELECT * FROM producto WHERE id_pro='$pro'";
	$eject = $con->query($sqli);
	while($fila = $eject->fetch_array()): 
////////////////////////////
if (empty($_POST['nombre_pro'])) {
		$nombre_pro=$fila['nombre_pro'];
}else{
	$nombre_pro = $_POST['nombre_pro'];
}
////////////////////////////////
if (empty($_POST['categoria'])) {
		$categoria=$fila['categoria'];
}else{
	$categoria = $_POST['categoria'];
}
////////////////////	
if (empty($_POST['descripcion'])) {
		$descripcion=$fila['descripcion'];
}else{
	$descripcion = $_POST['descripcion'];
	}
////////////////////	
if (empty($_POST['precio'])) {
		$precio=$fila['precio'];
}else{
	$precio = $_POST['precio'];
	}
//////////////////////
if (empty($_FILES['archivo'])){
		$nombreArchivo=$fila['image_pro'];
}else{
	$formatos = array('.jpg', '.png');
    $nombreArchivo = $_FILES['archivo']['name'];
    $nombreTmpArchivo = $_FILES['archivo']['tmp_name'];
    $ext = substr($nombreArchivo, strrpos($nombreArchivo, '.'));
    	if (in_array($ext, $formatos)){
        if(move_uploaded_file($nombreTmpArchivo, "images/$nombreArchivo")){
        	print "<script>alert(Imagen subida con éxito);window.location='../artesa/formsArtesa/catalogo.php';</script>";
        }else{
        	print "<script>alert(Error al subir imagen, comprueba el directorio del archivo);</script>";
        }
        }else{
         	print "<script>alert(Formato de imagen no aceptado, intenta con JPG y PNG)</script>";
        }
	$imagen=$nombreTmpArchivo;
	}
	endwhile;
	$departamento = $_SESSION['departamento'];
	$id_user = $_SESSION['id_user'];
	$enter = "UPDATE producto SET nombre_pro='$nombre_pro', image_pro='images/$nombreArchivo', descripcion='$descripcion', precio='$precio', departamento='    			$departamento', categoria='$categoria' WHERE id_pro = '$pro'";
	$query = $con->query($enter);
	if ($query === true)
		header("location: ../formsArtesa/catalogo.php");
	else
		print "Error al agregar producto";
    
asked by Luis Rivera 17.08.2017 в 02:37
source

1 answer

2

You have an error in the way you detect whether the file was sent or not:

if (empty($_FILES['archivo'])){

The content of $_FILES['archivo'] when a file is not sent is:

array (
  'archivo' => 
  array (
    'name' => '',
    'type' => '',
    'tmp_name' => '',
    'error' => 4,
    'size' => 0,
  ),
)

The content of $_FILES['archivo']['error'] (see documentation) is worth 4 ( UPLOAD_ERR_NO_FILE ) to indicate that there was no file and will be worth 2 ( UPLOAD_ERR_FORM_SIZE ) if the maximum size has been exceeded ( MAX_FILE_SIZE of a previous hidden field), etc.

So your empty will always give false , preventing you from maintaining the previous value.

You should use instead:

if (!isset($_FILES['archivo']['error']) || $_FILES['archivo']['error'] != 0) {

Also, I recommend you escape all the chains before sending them to an SQL query if you do not want to use prepared queries:

/* ¡¡IMPORTANTE SI NO USAS CONSULTAS PREPARADAS!! */
$pro = $con->real_escape_string($_GET['id_pro']);
$sqli = "SELECT * FROM producto WHERE id_pro='$pro'";
$eject = $con->query($sqli);

And also:

/* ¡¡IMPORTANTE SI NO USAS CONSULTAS PREPARADAS!! */
$nombre_pro = $con->real_escape_string($nombre_pro);
$categoria = $con->real_escape_string($categoria);
$descripcion = $con->real_escape_string($descripcion);
$precio = $con->real_escape_string($precio);
$nombreArchivo = $con->real_escape_string($nombreArchivo);
$departamento = $con->real_escape_string($_SESSION['departamento']);
$id_user = $con->real_escape_string($_SESSION['id_user']);
$enter = "
  UPDATE
    producto
  SET
    nombre_pro='$nombre_pro',
    image_pro='images/$nombreArchivo',
    descripcion='$descripcion', precio='$precio',
    departamento='$departamento',
    categoria='$categoria'
  WHERE
   id_pro = '$pro'
";
$query = $con->query($enter);
    
answered by 17.08.2017 / 10:03
source