Good morning everyone,
I have the following problem: I have a form where I retrieve a record from a database and an update button to edit the record in question.
The part of the form where I show the data of any registry works well, the page shows all the fields correctly. It is when I click on the update button that I can not edit the fields correctly, what it does is empty the registry completely.
I've been trying and testing for a while but I can not see where the error is. I show you what I have.
File where I collect the form data in includes / datoscliente.php
What I intend with this is not having to constantly write the variables of the fields of the forms.
<?php
$id = $_POST["idcliente"];
$datonombre = $_POST["nombre"];
$datoapellidos = $_POST["apellidos"];
$datoalias = $_POST["alias"];
$datotelefono = $_POST["telefono"];
$datomovil = $_POST["movil"];
$datoemail = $_POST["email"];
$datodireccion = $_POST["direccion"];
$datopoblacion = $_POST["poblacion"];
$datoprovincia = $_POST["provincia"];
$datocodigopostal = $_POST["codigopostal"];
$tratamientocapilar = $_POST["tratamientocapilar"];
$tratamientocorporal = $_POST["tratamientocorporal"];
$observaciones = $_POST["observaciones"];
?>
The form where I retrieve the data of a client based on the ID (edit.php)
This part works well and does not cause problems. I use a class called EditClient that contains a mysql statement that the client searches for by the ID.
<?php
// Incluimos la clase EditarCliente
require("includes/editarcliente.php");
// Instanciamos un objeto de la clase editar clientes
$cliente = new EditarCliente;
// Traemos el valor del cliente a editar
$idcliente = $_GET['id'];
// Creamos un objeto nuevo que traiga el cliente a editar usando el método EditClient de EditarCliente
$editarcliente = $cliente->EditClient($idcliente);
// Recorremos todo el array y guardamos cada elmento del campo en su correspondiente variable
foreach ($editarcliente as $elemento) {
$id = $elemento['idclientes'];
$nombre = $elemento['nombre'];
$apellidos = $elemento['apellidos'];
$alias = $elemento['alias'];
$telefono = $elemento['telefono'];
$movil = $elemento['movil'];
$email = $elemento['email'];
$direccion = $elemento['direccion'];
$poblacion = $elemento['poblacion'];
$provincia = $elemento['provincia'];
$codigopostal = $elemento['codigopostal'];
$tratamientocapilar = $elemento['tratamientocapilar'];
$tratamientocorporal = $elemento['tratamientocorporal'];
$observaciones = $elemento['observaciones'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html lang="es">
<head>
<?php
// Incluimos el header de la página
include("includes/header.php");
?>
</head>
<body>
<div class="container">
<header class="header">
<h1>Gestión Clientes <small>NNNN</small></h1>
</header>
</div>
<div class="container menu-create">
<form action="actualizarcliente.php" method="POST">
<div class="form-horizontal">
<div class="form-group">
<label class="col-lg-2 control-label">ID Cliente</label>
<div class="col-lg-10">
<input type="text" name="idcliente" id="idcliente" class="form-control input_size" value="<?php echo $id;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Nombre</label>
<div class="col-lg-10">
<input type="text" name="nombre" id="nombre" class="form-control input_size" value="<?php echo $nombre;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Apellidos</label>
<div class="col-lg-10">
<input type="text" name="apellidos" id="apellidos" class="form-control input_size" value="<?php echo $apellidos;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Alias</label>
<div class="col-lg-10">
<input type="text" name="alias" id="alias" class="form-control input_size" value="<?php echo $alias;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Teléfono</label>
<div class="col-lg-10">
<input type="number" name="telefono" id="telefono" class="form-control input_size" value="<?php echo $telefono;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Móvil</label>
<div class="col-lg-10">
<input type="number" name="movil" id="movil" class="form-control input_size" value="<?php echo $movil;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">E-mail</label>
<div class="col-lg-10">
<input type="email" name="email" id="email" class="form-control input_size" value="<?php echo $email;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Dirección</label>
<div class="col-lg-10">
<input type="text" name="direccion" id="direccion" class="form-control input_size" value="<?php echo $direccion;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Población</label>
<div class="col-lg-10">
<input type="text" name="poblacion" id="poblacion" class="form-control input_size" value="<?php echo $poblacion;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Provincia</label>
<div class="col-lg-10">
<input type="text" name="provincia" id="provincia" class="form-control input_size" value="<?php echo $provincia;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Código Postal</label>
<div class="col-lg-10">
<input type="number" name="codigopostal" id="codigopostal" class="form-control input_size" value="<?php echo $codigopostal;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Tratamiento Capilar</label>
<div class="col-lg-10">
<input type="text" name="tratamientocapilar" id="tratamientocapilar" class="form-control input_size" value="<?php echo $tratamientocapilar;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Tratamiento Corporal</label>
<div class="col-lg-10">
<input type="text" name="tratamientocorporal" id="tratamientocorporal" class="form-control input_size" value="<?php echo $tratamientocorporal;?>" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Observaciones</label>
<div class="col-lg-10">
<input type="text" name="observaciones" id="observaciones" class="form-control input_size" value="<?php echo $observaciones;?>" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" name="enviar" value="Salvar" class="btn btn-default">Salvar</button>
</div>
</div>
</div>
</form>
</div>
</body>
</html>
Class ModifyClient (modifyclient.php)
I have this class to modify the client with this code:
<?php
// Incluimos los datos del formulario para PHP
require("datosclientes.php");
// Incluimos la clase conexión para crear una que herede de Conexion
require("conexion.php");
/*
* Clase modificar cliente que hereda las propiedades de la clase conexion
*/
class ModificarCliente extends Conexion {
// Constructor de la clase
function __construct(){
// Constructor de la super clase
parent::__construct();
}
// Método para guardar las modificaciones del cliente
public function ModificarCliente($id) {
// try and catch para capturar errores
try {
// Sentencia SQL
$sql = "UPDATE clientes SET
nombre = '$datonombre',
apellidos = '$datoapellidos',
alias = '$datoalias',
telefono = '$datotelefono',
movil = '$datomovil',
email = '$datoemail',
direccion = '$datodireccion',
poblacion = '$datopoblacion',
provincia = '$datoprovincia',
codigopostal = '$datocodigopostal',
tratamientocapilar = '$tratamientocapilar',
tratamientocorporal = '$tratamientocorporal',
observaciones = '$observaciones'
WHERE idclientes = '$id'";
// Guardamos la variable SQL y preparamos la consulta con la conexion de la base de datos
$query = $this->conexion_db->prepare($sql);
// Ejecutamos la consulta y la guardamos en un array
$query->execute(array());
// Guardamos el resultado en una variable
$resultado = $query;
// Cerramos la query
$query->closeCursor();
// Devolvemos los resultados a la función
return $resultado;
// Vaciamos el objeto
$this->conexion_db = null;
} catch (Exception $e) {
echo "Error en la ejecución de la consulta<br>";
echo "Mensaje: " . $e->GetMessage() . "<br>";
echo "Línea: " . $e->getLine();
}
}
}
?>
And finally I have the updateclient.php page where I instantiate the ModificarCliente
class using the ModificarCliente
method with the $id
parameter I have from the form edit.php
<?php
// Incluimos los datos del formulario para PHP
require("includes/datosclientes.php");
// Incluimos la clase Modificar cliente
require("includes/modificarcliente.php");
// Instanciamos un objeto de modificar cliente
$modificarcliente = new ModificarCliente;
$modificarcliente->ModificarCliente($id);
echo $id;
if($modificarcliente == false) {
echo "error";
} else {
echo "Cliente modificado";
}
?>
The problem
When I click on the Save button in the form edit.php what the program does is empty the record in question. That is, if I am editing the client with id 12, it empties all the client fields with id 12.
When I run it, it shows me an error for each field of the form:
Notice: Undefined variable: name in C: \ wamp64 \ www \ elena \ includes \ modifyclient.php on line 31
Notice: Undefined variable: data in C: \ wamp64 \ www \ elena \ includes \ modifycliente.php on line 32
And then the errors I show with the class ModificarCliente
indicate the following:
Message: SQLSTATE [HY000]: General error
However, the if does it and shows the text "Modified Client".
I have an include with the data of the forms, but for the errors that it marks is as if PHP can not rescue the data from the form editar.php
.
What am I doing wrong?
var_dump ($ sql)
C:\wamp64\www\nnn\includes\modificarcliente.php:48:string 'UPDATE clientes SET
nombre = 'David',
apellidos = 'nnn',
alias = 'nnn',
telefono = '000000000',
movil = '00000',
email = '[email protected]',
direccion = 'nnnn',
poblacion = 'nnnn',
provincia = 'p',
codigopostal = '222',
tratamientocapilar = 'prueba',
'... (length=645)
var_dump ($ data)
array (size=15)
'idcliente' => string '11' (length=2)
'nombre' => string 'David' (length=5)
'apellidos' => string 'nnn' (length=15)
'alias' => string 'nnn' (length=6)
'telefono' => string '000000000' (length=9)
'movil' => string '00000' (length=5)
'email' => string '[email protected]' (length=7)
'direccion' => string 'nnn' (length=14)
'poblacion' => string nnn' (length=12)
'provincia' => string 'p' (length=1)
'codigopostal' => string '222' (length=3)
'tratamientocapilar' => string 'prueba' (length=6)
'tratamientocorporal' => string 'prueba' (length=6)
'observaciones' => string 'prueba' (length=6)
'enviar' => string 'Salvar' (length=6)