Undefined offset in PHP

1

Let's see if you can help me that for many laps I give, I can not find the error. The attached code returns me:

  

Notice: "Undefined offset: 0 in   /var/www/vhosts/16/181528/webspace/httpdocs/sur-madrid.es/alumnos/Richard/Marcas/Administrator/aceptar-edicion.php   on line 56 Notice: Undefined index: in   /var/www/vhosts/16/181528/webspace/httpdocs/sur-madrid.es/alumnos/Richard/Marcas/Administrator/aceptar-edicion.php   on line 56 "

I understand that the error occurs on line 56 ( $sql = "UPDATE marcas SET " . $fila[$i] . " = '" . $_POST[$fila[$i]] . "' WHERE nombre = '" . $marca . "'"; ) until I get there, but I do not get to know the reason for the failure, I hope you can help me, thanks. The code is as follows:

<body>
<?php
    ini_set('display_errors', 'On');

        // Valor por defecto en PHP
        // Muestra todos los errores menos las notificaciones
        error_reporting(E_ALL ^ E_NOTICE);

        // Muestro todos los errores
        error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
        error_reporting(E_ALL);
        error_reporting(-1);

        // Muestro todos los errores, incluso los estrictos
        error_reporting(E_ALL | E_STRICT);

        // No muestra ningún error
        error_reporting(0);

        // También se puede usar la función ini_set
        ini_set('error_reporting', E_ALL);


     //// Recoger variables ////

    $marca = $_GET['marca'];

    ////Datos db
    $usuario = "usuario";
    $password = "pass";
    $servidor = "server";
    $basededatos = "bd";

    ////Crear conexion
    $conexion = mysqli_connect($servidor, $usuario, $password)
    or die("No se ha podido conectar a la base de datos");

    ////Seleccionar db
    $db = mysqli_select_db($conexion, $basededatos)
    or die("Uppppss! No se ha podido conectar a la base de datos");

    ////Establecer y realizar consulta
    $sql = "SELECT * FROM marcas";

    $resultado = mysqli_query($conexion, $sql);
    $fila = mysqli_fetch_assoc($resultado);

    for($i=0; $i<=mysqli_num_fields($resultado); $i++){

        $sql = "UPDATE marcas SET " . $fila[$i] . " = '" . $_POST[$fila[$i]] . "' WHERE nombre = '" . $marca . "'";
        $resultado = mysqli_query($conexion, $sql);

        }

    ////Cerrar conexion 
    mysqli_close($conexion);
?>

    
asked by RichardSC 09.05.2018 в 10:59
source

1 answer

0
  

int mysqli_num_fields (mysqli_result $ result)   Returns the number of fields in the specified resultset.

Therefore, your error is in this loop, never the index $i can be equal to the number of fields since if there are 3 fields the index is only within the range up to number 2 (0,1,2 ) try using < instead of <= .

for($i=0; $i<mysqli_num_fields($resultado); $i++){

      $sql = "UPDATE marcas SET " . $fila[$i] . " = '" . $_POST[$fila[$i]] . "' WHERE nombre = '" . $marca . "'";
      $resultado = mysqli_query($conexion, $sql);

}

Documentation

    
answered by 09.05.2018 в 12:25