UPDATE MYSQL WITH PHP

0

I'm trying to do an update in mysql from php, but I'm finding it difficult, since I want to collect the values of a form in which their names are put with the name of the fields of the columns, I do not know how to explain Well, I pass code to you:

<body>
<div align="center" style="font-size:40px">
    <form method="post" action="aceptar-edicion.php">
        <fieldset style="width:70%">
            <legend>Editar marca</legend>
            <?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 = $_POST['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 = "SHOW COLUMNS FROM marcas";

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

                    while($fila = mysqli_fetch_assoc($resultado)){

                        echo "<label>" . ($fila['Field']) . "   " . "<input type='text' name='" . $fila['Field'] . "' id='" . $fila['Field'] . "'</label></br>";

                    }//end while

                    ////Cerrar conexion 
                    mysqli_close($conexion);
            ?>
            </br>
            <input type="submit" value="Aceptar cambio"/>
        </fieldset>
    </form>
</div>

As you can see, I want to make labels with their respective inputs by putting the name of each column of the mysql table. That is, if I have the columns in my table "name, address, phone, email" I want to leave a label with that value and an input next to it with the "name" and the "id" called the field, until then good.

Now, I want to collect the values of those input and put them in a table already created, but I would not know how to do it, because I would have to read the columns of the table again.

<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 = $_POST['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
    ////////////////////////AQUÍ ES DONDE NO SE QUÉ HACER//////////////////////////
    $sql = "UPDATE marcas SET ";
    for($i=0; $i<= ; $i++){
        echo $fila[$i] . " = '" . $_POST[$fila[$i]] . "', ";
        }
        echo " WHERE nombre = " . $marca . ";"; 

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


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

I do not know how I could do it, it's very difficult for me, I hope you can help me, thank you.

    
asked by RichardSC 08.05.2018 в 13:38
source

1 answer

1

As I said in comments, POST creates an associative array in which the key equals the label name of each input , while the value is the value written in it.

Then, you could read that array and set the values.

Although your code does not seem to fulfill what you say, because the column marca you equal to an assumed input name equal to nombre . I've written the code in that sense, but you should pay attention to what that column is really called in your table, because the correct functioning will depend on it.

It would be something like this:

$arrPost=$_POST;
$marca=$POST_["marca"];

$sql="UPDATE marcas SET ";
foreach ($arrPost as $k=>$v){
    /*Se supone que la columna marca no entra en los SET*/
    if($k!="nombre"){
        $sql.=$k."='".$v."'";   
    }   
}
$sql.=" WHERE nombre = '" . $marca . "'";
  

NOTE: This code is vulnerable to SQL injection. This response has been written responding to the requirements of the OP. Agree on   this case apply prepared queries to prevent attacks from   SQL injection.

    
answered by 08.05.2018 в 14:12