Está en la página 1de 4

//conexion

<?php
try{
beginTransaction();
$dns=MariaDB,172.20.14.56,3306, empleado, asWSA123,proyecto);
}catch(PDOException Exception){
echo'Error de conexion';
}

//controller
<?php
class Producto extends Sistema
{
public function get($id = null)
{
$this->db();
if (is_null($id)) {
$sql = "select * from producto p left join marca m
on p.id_marca = m.id_marca ";
$st = $this->db->prepare($sql);
$st->execute();
$data = $st->fetchAll(PDO::FETCH_ASSOC);
} else {
$sql = "select * from producto p left join marca m
on p.id_marca = m.id_marca where p.id_producto=:id";
$st = $this->db->prepare($sql);
$st->bindParam(":id", $id, PDO::PARAM_INT);
$st->execute();
$data = $st->fetchAll(PDO::FETCH_ASSOC);
}

return $data;
}

public function new($data)


{
$this->db();
$sql = "INSERT INTO producto (producto, precio, id_marca)
VALUES (:producto, :precio, :id_marca)";
$st = $this->db->prepare($sql);
$st->bindParam(":producto", $data['producto'], PDO::PARAM_STR);
$st->bindParam(":precio", $data['precio'], PDO::PARAM_NUMERIC);
$st->bindParam(":id_marca", $data['id_marca'], PDO::PARAM_INT);
$st->execute();
$rc = $st->rowCount();
return $rc;
}

public function delete($id)


{
$rc=0;
try{
$s->beginTransaction();
$this->db();
$sql = "DELETE FROM producto WHERE id_producto=:id";
$st = $this->db->prepare($sql);
$st->bindParam(":id", $id, PDO::PARAM_INT);
$st->execute();
$rc = $st->rowCount();
}catch(PDOException Exception){
s->rollback();
echo 'Error';
}
return $rc;
}

public function edit($id, $data)


{

$this->db();
$sql = "UPDATE proyecto
SET producto =:producto, precio =:precio,
id_marca =:id_marca
where id_producto =:id";
$st = $this->db->prepare($sql);
$st->bindParam(":producto", $data['producto'], PDO::PARAM_STR);
$st->bindParam(":precio", $data['precio'], PDO::PARAM_FLOAT);
$st->bindParam(":id_marca", $data['id_marca'], PDO::PARAM_INT);
$st->execute();
$rc = $st->rowCount();
return $rc;
}

$producto = new Producto;

//index
<h1>Producto</h1>
<label>Se encontraron <?php echo $nReg ?> registros. </label>
<table>
<thead>
<tr>
<th>#</th>
<th>Nombre de la Marca</th>
<th>Nombre del producto</th>
<th>Precio</th>
<th>Opciones</th>
</tr>
</thead>
<tbody>
<?php $nReg = 0;
foreach ($data as $key => $producto) :
$nReg++;
?>
<tr>
<th>
<?php echo $producto["id_producto"] ?>
</th>
<th>
<?php echo $producto["producto"] ?>
</th>
<th>
<?php echo $producto["precio"] ?>
</th>
</th>
<th>
<div>
<a href="producto.php?action=edit&id=<?php echo
$producto["id_producto"] ?>" type="button">Modificar</a>
<a href="producto.php?action=delete&id=<?php echo
$producto["id_producto"] ?>" type="button">Eliminar</a>
</div>
</th>
</tr>
<?php endforeach; ?>
</tbody>
</table>

//form
<h1>
<?php echo ($action == 'edit') ? 'Modificar' : 'Nueva'; ?> Producto
<?php echo $data[0]['producto']; ?>
</h1>

<form method="POST" action="producto.php?action=<?php echo $action; ?>&id=<?php


echo($data[0]['id_producto']) ?>">
<div>
<label>Producto</label>
<input type="text" name="data[producto]" value="<?php echo isset($data[0]
['producto']) ? $data[0]['producto'] : ''; ?>" />
</div>

<div>
<label>Precio</label>
<input type="number" name="data[producto]" value="<?php echo isset($data[0]
['producto']) ? $data[0]['producto'] : ''; ?>" />
</div>

<div class="col-2">
<label for="id_privilegio">privilegio</label>
</div>
</div>
<div class="row">
<div class="col-2">
<select name="data[id_marca]" required="required">
<?php
$selected = " ";
foreach ($data_marca as $key => $marca):
if ($marca['id_marca'] == $data[0]['id_marca']):
$selected = "selected";
endif;
?>
<option value="<?php echo $marca['id_marca']; ?>" <?php echo
$selected; ?>>
<?php echo $marca['marca']; ?></option>
<?php $selected = " "; endforeach; ?>
</select>
</div>
<div>
<input type="hidden" name="data[id_producto]" value="<?php echo($data[0]
['id_producto']) ?>">
<?php
if ($action == 'edit'): ?>
<input type="hidden" name="data[id_producto]"
value="<?php echo isset($data[0]['id_producto']) ? $data[0]
['id_producto'] : ''; ?>"/>

<?php endif; ?>


<input type="submit" name="enviar" value="Guardar"/>

</div>
</form>

También podría gustarte