En esta oportunidad voy a realizar una actualización a los datos de una tabla creada en mysql, usando la palabra reservada UPDATE.
Para este ejemplo voy a crear una tabla llamada clientes, y 2 archivos php. La conexión la pueden ver en este post que publique hace días.
Clientes.sql
CREATE TABLE IF NOT EXISTS `clientes` ( `id_cliente` int(11) NOT NULL AUTO_INCREMENT, `cliente` varchar(200) NOT NULL, `direccion` varchar(200) NOT NULL, `dni` char(8) NOT NULL, `telefono` char(9) DEFAULT NULL, PRIMARY KEY (`id_cliente`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
index.php
<?php
include_once("class/Conexion.php");
include_once("class/Clientes.php");
$link= new Conexion("localhost","root","","blog");
$clientes = new Clientes();
$array_cliente = $clientes->GetClientes();
switch ($_GET['accion']) {
case "edit" : $datos = $clientes->GetClientes($_GET['id']);
break;
case "update" : $clientes->UpdateClientes($_GET['id']);
break;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
</head>
<body>
<table width="700" border="1" cellpadding="0" cellspacing="0">
<tr align="center">
<td width="172">CLIENTE</td>
<td width="201">DIRECCIÓN</td>
<td width="109">DNI</td>
<td width="107">TELEFONO</td>
<td width="99">OPCIONES</td>
</tr>
<?php
if ($array_cliente) {
foreach ($array_cliente as $value):
?>
<tr>
<td><?php echo $value['cliente'] ?></td>
<td><?php echo $value['direccion'] ?></td>
<td align="center"><?php echo $value['dni'] ?></td>
<td align="center"><?php echo $value['telefono'] ?></td>
<td align="center"><a href="index.php?accion=edit&id=<?php echo $value['id_cliente'] ?>">EDITAR</a></td>
</tr>
<?php
endforeach;
}
?>
</table>
<br />
<form name="frmclientes" method="post" action="index.php?accion=update&id=<?php echo $datos[0]['id_cliente'] ?>">
<table width="247">
<tr>
<td width="69"><label>Cliente:</label></td>
<td width="166"><input type="text" name="cliente" value="<?php echo $datos[0]['cliente'] ?>" /></td>
</tr>
<tr>
<td><label>Dirección:</label></td>
<td><input type="text" name="direccion" value="<?php echo $datos[0]['direccion'] ?>" /></td>
</tr>
<tr>
<td><label>Dni:</label></td>
<td><input type="text" name="dni" value="<?php echo $datos[0]['dni'] ?>" /></td>
</tr>
<tr>
<td><label>Telefono:</label></td>
<td><input type="text" name="telefono" value="<?php echo $datos[0]['telefono'] ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btneditar" value="EDITAR DATOS" /></td>
</tr>
</table>
</form>
</body>
</html>
Clientes.php
class Clientes {
private $datos = array();
public function GetClientes($id=0) {
$where = ($id > 0) ? "where id_cliente=" . $id : "";
$sql = mysql_query("SELECT * FROM clientes " . $where . " ORDER BY 1");
while ($res = mysql_fetch_array($sql)) {
$datos[] = $res;
}
return $datos;
}
public function UpdateClientes($id=0) {
$sql = mysql_query("UPDATE clientes SET cliente = '" . $_POST["cliente"] . "', direccion = '" . $_POST["direccion"] . "', dni = '" . $_POST["dni"] . "', telefono = '" . $_POST["telefono"] . "' WHERE id_cliente = " . $id . "");
header("location:index.php");
}
}




0 comentarios:
Publicar un comentario