I have the following code that generates a table in php returning me the data stored 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 modify.php page to modify the data if necessary. With idC=$clients[id] I get the ID of the client that I want to modify. In my modify.php file 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 on the line where I have my sql query to update the data. In case I use GET or POST both in the form and when obtaining the new name, etc., the error it throws me is Undefined index .
Greetings and thanks.
['nuevoNombre']
? It is wrong it should be$_POST['nuevoNombre']
, if after this it shows theThis usually happens because when loading the modify.php it executes the PHP code and inside it, it looks for the indices that have not yet been sent by POST , there is the detail.
To solve this, you could verify the request type if it is POST using the key
REQUEST_METHOD
and$_SERVER
then perform the operations depending on the Type.Well, a solution to the problem would be to directly assign the GET
idCliente
value to a hidden input type so as not to lose the value when doing the POST , I changed the PHP code to obtain the id from the hidden fieldPOST
After updating and verifying that the update was done in the db (else) , you could redirect to the page you want in the following way (taking into account the distribution of your directories)