Array to string conversion. Modify mysql data with php

2

I have the following code that generates a table in php, returning the data saved in a database in mysql

require("conexion.php");

$consulta = mysqli_query($conexion, "SELECT * FROM clientes");

if (!$consulta) {
    die("Fallo al realizar la consulta");
}

echo ("<table border='1px'>");
echo ("<th> NOMBRE </th> <th> MEDIO DE PAGO </th> <th>PLAN CONTRATADO</th> <th> MODIFICAR </th> <th> ELIMINAR </th>");
while ($clientes = mysqli_fetch_array($consulta)) {
    echo ("<tr>");
    echo ("<td>$clientes[nombre]</td>");
    echo ("<td>$clientes[medioPago]</td>");
    echo ("<td>$clientes[plan]</td>");
    echo ("<td><a href='modificar.php?idC=$clientes[id]'>Modificar </a></td>");
    echo ("<td><a href='eliminar.php'>Eliminar </a></td>");
    echo ("</tr>");
}
echo ("</table>");

As you can see, each record also generates a link that takes me to the page modify.php to modify the data if necessary. With idC = $ clients [id] I get the ID of the client that I want to modify. In my file modify.php I have the following code

<form action="modificar.php">
        <label>Nuevo nombre: </label>
        <input type="text" name="nuevoNombre" placeholder="Ingrese el  nuevo nombre">

        <label>Medio de pago: </label>
        <select name="nuevoMedioPago">
            <option value="tarjeta">Tarjeta de credito</option>
            <option value="rapipago">Rapipago - Pagofacil</option>
            <option value="deposito">Deposito</option>
            <option value="efectivo">Efectivo</option>
            <option value="otro">Otro</option>
        </select>

        <label>Nuevo plan contratado: </label>
        <select name="nuevoPlanContratado">
            <option value="basico">Basico</option>
            <option value="avanzado">Avanzado</option>
            <option value="profesional">Profesional</option>
        </select>

        <input type="submit" value="Modificar">
    </form>


    <?php
    require("conexion.php");
    $idCliente = $_GET['idC'];

    echo ("Modificaras los datos del cliente cuya ID es: $idCliente <br>" );
    $consulta = mysqli_query($conexion, "SELECT * FROM clientes WHERE id = $idCliente");
    while ($mostrar = mysqli_fetch_array($consulta)) {
        echo ("Nombre: $mostrar[nombre] <br>");
        echo ("Medio de pago: $mostrar[medioPago] <br>");
        echo ("Plan: $mostrar[plan] <br>");
    }

    ////////////////////////////ACTUALIZACION////////////////////////////
    $nuevoNombre = ['nuevoNombre'];
    $nuevoMedioPago = ['nuevoMedioPago'];
    $nuevoPlanContratado = ['nuevoPlanContratado'];

    $actualizar = mysqli_query($conexion, "UPDATE clientes SET nombre='$nuevoNombre' , medioPago='$nuevoMedioPago' , plan='$nuevoPlanContratado' WHERE id = $idCliente");

    if (!$actualizar) {
        die("Fallo al actualizar los datos");
    }



    ?>

Which throws me an error in the line where I have my sql query to update the data. In case I use GET or POST both in the form and at the time of obtaining the new name, etc. the error that throws at me is Undefined index .

Greetings and thanks.

    
asked by Lucas. D 19.05.2017 в 21:29
source

1 answer

1

['nuevoNombre'] ? Incorrect should be $_POST['nuevoNombre'] , if after this it shows the

  

Undefined index.

That usually happens because when loading the modify.php it executes the PHP code and inside it, looks for the indices that have not yet been sent by POST , there the detail.

To solve this, you could verify the type of request if it is POST using the key REQUEST_METHOD of $_SERVER to then perform the operations depending on the type.

if($_SERVER['REQUEST_METHOD']=='POST'){
   /* Aquí debería validar con isset las claves, por Ejemplo . si no puede 
    volver a tener problemas*/
    $idCliente = $_POST['idC'];
    $nuevoNombre = $_POST['nuevoNombre'];
    $nuevoMedioPago = $_POST['nuevoMedioPago'];
    $nuevoPlanContratado = $_POST['nuevoPlanContratado'];

    $actualizar = mysqli_query($conexion, "UPDATE clientes SET nombre='$nuevoNombre' , medioPago='$nuevoMedioPago' , plan='$nuevoPlanContratado' WHERE id = $idCliente");

    if (!$actualizar) {
        die("Fallo al actualizar los datos");
    }
    else
       header("Location: nombredetuarchivo.php")
}

Well, a solution to the problem of idCliente would be the value by GET directly assign it to a input type hidden to not lose the value when doing the POST , I changed the code PHP , to get the id by POST of the field hidden

  <form action="modificar.php" method="POST" >
  <input type="hidden" name="idC" value="<?php echo (isset($_GET['idC'])) ? $_GET['idC'] : 0 ?>">

After updating and verifying that the update was performed in the bd (else) , you could redirect to the desired page in the following way (taking into account the distribution of your directories) )

header("Location: nombredetuarchivo.php")
  

As a recommendation read Avoid Injection of SQL code

    
answered by 19.05.2017 / 21:51
source