Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Hacer UPDATE en una tabla e INSERT a otra tabla

Estas en el tema de Hacer UPDATE en una tabla e INSERT a otra tabla en el foro de PHP en Foros del Web. Saludos cordiales, la misma es para consultar lo siguiente: Poseo el siguiente codigo Código PHP: public function  Actualizar ( Usuario $data )     {         try          {              $sql  ...
  #1 (permalink)  
Antiguo 21/12/2016, 11:20
(Desactivado)
 
Fecha de Ingreso: septiembre-2008
Ubicación: Maracay edo Aragua Venezuela
Mensajes: 48
Antigüedad: 15 años, 7 meses
Puntos: 3
Pregunta Hacer UPDATE en una tabla e INSERT a otra tabla

Saludos cordiales, la misma es para consultar lo siguiente:

Poseo el siguiente codigo

Código PHP:
public function Actualizar(Usuario $data)
    {
        try 
        {
            
$sql "UPDATE ingreso SET 
                        rif          = ?, 
                        nombre        = ?,
                        producto            = ?, 
                        sistema_enc = ?,
                        nro_cupon = ?,
                        fecha_envio = ?,
                        nota = ?
                    WHERE id = ?"
;

            
$this->pdo->prepare($sql)
                 ->
execute(
                array(
                    
$data->__GET('Rif'), 
                    
$data->__GET('Nombre'), 
                    
$data->__GET('Producto'),
                    
$data->__GET('sistema_enc'),
                    
$data->__GET('nro_cupon'),
                    
$data->__GET('fecha_envio'),
                    
$data->__GET('nota'),
                    
$data->__GET('id')
                    
                    )
                );
        } catch (
Exception $e
        {
            die(
$e->getMessage());
        }
    } 
Este codigo funciona perfectamente actualizando la tabla "ingreso" pero me gustaria que al mismo tiempo se añadan solo algunos datos a la tabla "vitacora", algo como:


Código PHP:
$sql "INSERT INTO vitacora (rif,nota) VALUES (?, ?)";
            
$this->pdo->prepare($sql)
                 ->
execute(
                array(
                    
$data->__GET('Rif'), 
                    
$data->__GET('nota'),
                ) 
es posible ejecutar ambas sentencias en una misma funcion..??

Última edición por JESUMINISTROSYMAS; 21/12/2016 a las 11:24 Razón: la web me saco y borro parte del texto no lo guardo en el borrador
  #2 (permalink)  
Antiguo 21/12/2016, 11:56
Avatar de petit89  
Fecha de Ingreso: marzo-2011
Mensajes: 1.135
Antigüedad: 13 años, 1 mes
Puntos: 170
Respuesta: Hacer UPDATE en una tabla e INSERT a otra tabla

Si es posible
__________________
█ WebHosting / Reseller a bajo costo | Uptime Garantizado | Soporte en Español e Ingles
¿Te sirvió la respuesta? Deja un +1 (Triangulo negro al lado derecho)
  #3 (permalink)  
Antiguo 21/12/2016, 12:44
 
Fecha de Ingreso: noviembre-2003
Ubicación: Zaragoza, España
Mensajes: 1.257
Antigüedad: 20 años, 5 meses
Puntos: 154
Respuesta: Hacer UPDATE en una tabla e INSERT a otra tabla

Hola JESUMINISTROSYMAS,

Hasta dónde yo sé, deberías pasarle por parámetro también el nombre de la tabla y, obviamente, hacer los cambios necesarios en la función.
  #4 (permalink)  
Antiguo 21/12/2016, 15:31
(Desactivado)
 
Fecha de Ingreso: septiembre-2008
Ubicación: Maracay edo Aragua Venezuela
Mensajes: 48
Antigüedad: 15 años, 7 meses
Puntos: 3
Pregunta Respuesta: Hacer UPDATE en una tabla e INSERT a otra tabla

Ampliare lo que tengo:

INDEX:PHP
Código PHP:
<?php
require_once 'usuario.entidad.php';
require_once 
'usuario.model.php';

// Logica
$alm = new Usuario();
$model = new UsuarioModel();

if(isset(
$_REQUEST['action']))
{
    switch(
$_REQUEST['action'])
    {
        case 
'actualizar':
            
$alm->__SET('id',          $_REQUEST['id']);
            
$alm->__SET('Rif',         $_REQUEST['Rif']);
            
$alm->__SET('Nombre',      $_REQUEST['Nombre']);
            
$alm->__SET('Producto',    $_REQUEST['Producto']);
            
$alm->__SET('sistema_enc'$_REQUEST['sistema_enc']);
            
$alm->__SET('nro_cupon',   $_REQUEST['nro_cupon']);
            
$alm->__SET('fecha_envio'$_REQUEST['fecha_envio']);
            
$alm->__SET('nota',        $_REQUEST['nota']);

            
$model->Actualizar($alm);
            
header('Location: index.php');
            break;

        case 
'registrar':
            
$alm->__SET('Rif',          $_REQUEST['Rif']);
            
$alm->__SET('Nombre',       $_REQUEST['Nombre']);
            
$alm->__SET('Producto',     $_REQUEST['Producto']);
            
$alm->__SET('sistema_enc',  $_REQUEST['sistema_enc']);
            
$alm->__SET('nro_cupon',    $_REQUEST['nro_cupon']);
            
$alm->__SET('fecha_envio',  $_REQUEST['fecha_envio']);
            
$alm->__SET('nota',         $_REQUEST['nota']);

            
$model->Registrar($alm);
            
header('Location: index.php');
            break;        

        case 
'eliminar':
            
$model->Eliminar($_REQUEST['id']);
            
header('Location: index.php');
            break;

        case 
'editar':
            
$alm $model->Obtener($_REQUEST['id']);
            break;
    }
}

?>

<!DOCTYPE html>
<html lang="es">
<head>
    <title>SIGENVIOS</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" >
    <link rel="stylesheet" type="text/css" href="../../../admin/config/css/estilo2.css">
</head>
    <body style="padding:15px;">

        <div class="pure-g">
            <div class="pure-u-1-12">
                
                <form action="?action=<?php echo $alm->id 'actualizar' 'registrar'?>" method="post" class="pure-form "style="margin-bottom:30px;">
                    <input type="hidden" name="id" value="<?php echo $alm->__GET('id'); ?>" />
                    
                    <table style="width:500px;">
                        <tr>
                            <th style="text-align:left;">Rif</th>
                            <td><input type="text" name="Rif" value="<?php echo $alm->__GET('Rif'); ?>" style="width:100%;" /></td>
                        </tr>
                        <tr>
                            <th style="text-align:left;">Nombre</th>
                            <td><input type="text" name="Nombre" value="<?php echo $alm->__GET('Nombre'); ?>" style="width:100%;" /></td>
                        </tr>
AQUI VA EL RESTO DEL FORMULARIO LO BORRE POR SER MUY LARGO
                        <tr>
                            <td colspan="2">
                                <button type="submit" class="pure-button pure-button-primary">Guardar</button>
                            </td>
                        </tr>
                    </table>
                </form>

                <table class="pure-table pure-table-horizontal">
                    <thead>
                        <tr>
                            <th style="text-align:left;">Rif</th>
                            <th style="text-align:left;">Nombre</th>
                            <th style="text-align:left;">Producto</th>
                            <th style="text-align:left;">Encomienda</th>
                            <th style="text-align:left;">Nro Cupon</th>
                            <th style="text-align:left;">Fecha Envio</th>
                            <th style="text-align:left;">Nota</th>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                    <?php foreach($model->Listar() as $r): ?>
                        <tr>
                            <td><?php echo $r->__GET('Rif'); ?></td>
                            <td><?php echo $r->__GET('Nombre'); ?></td>
                            <td><?php echo $r->__GET('Producto'); ?></td>
                            <td><?php echo $r->__GET('sistema_enc'); ?></td>
                            <td><?php echo $r->__GET('nro_cupon'); ?></td>
                            <td><?php echo $r->__GET('fecha_envio'); ?></td>
                            <td><?php echo $r->__GET('nota'); ?></td>
                            <td>
                                <a href="?action=editar&id=<?php echo $r->id?>">Editar</a>
                            </td>
                            <td>
 <!-- esta linea activa la opcion de borrar row de mysql-->
 <a href="?action=eliminar&id=<?php echo $r->id?>">Eliminar</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </table>     
              
            </div>
        </div>

    </body>
</html>
USUARIO.MODEL.PHP
Código PHP:
<?php
class UsuarioModel
{
    private 
$pdo;

    public function 
__CONSTRUCT()
    {
        try
        {
            
$this->pdo = new PDO('mysql:host=localhost;dbname=datos''MI USUARIO''MI CLAVE');

            
$this->pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);                
        }
        catch(
Exception $e)
        {
            die(
$e->getMessage());
        }
    }

    public function 
Listar()
    {
        try
        {
            
$result = array();

            
$stm $this->pdo->prepare("SELECT * FROM ingreso ORDER BY id DESC  LIMIT 5");
            
$stm->execute();

            foreach(
$stm->fetchAll(PDO::FETCH_OBJ) as $r)
            {
                
$alm = new Usuario();

                
$alm->__SET('id'$r->id);
                
$alm->__SET('Rif'$r->rif);
                
$alm->__SET('Nombre'$r->nombre);
                
$alm->__SET('Producto'$r->producto);
                
$alm->__SET('sistema_enc'$r->sistema_enc);
                
$alm->__SET('nro_cupon'$r->nro_cupon);
                
$alm->__SET('fecha_envio'$r->fecha_envio);
                
$alm->__SET('nota'$r->nota);

                
$result[] = $alm;
            }

            return 
$result;
        }
        catch(
Exception $e)
        {
            die(
$e->getMessage());
        }
    }

    public function 
Obtener($id)
    {
        try 
        {
            
$stm $this->pdo
                      
->prepare("SELECT * FROM ingreso WHERE id = ?");
                      

            
$stm->execute(array($id));
            
$r $stm->fetch(PDO::FETCH_OBJ);

            
$alm = new Usuario();

                
$alm->__SET('id'$r->id);
                
$alm->__SET('Rif'$r->rif);
                
$alm->__SET('Nombre'$r->nombre);
                
$alm->__SET('Producto'$r->producto);
                
$alm->__SET('sistema_enc'$r->sistema_enc);
                
$alm->__SET('nro_cupon'$r->nro_cupon);
                
$alm->__SET('fecha_envio'$r->fecha_envio);
                
$alm->__SET('nota'$r->nota);

            return 
$alm;
        } catch (
Exception $e
        {
            die(
$e->getMessage());
        }
    }

    public function 
Eliminar($id)
    {
        try 
        {
            
$stm $this->pdo
                      
->prepare("DELETE FROM ingreso WHERE id = ?");                      

            
$stm->execute(array($id));
        } catch (
Exception $e
        {
            die(
$e->getMessage());
        }
    }

    public function 
Actualizar(Usuario $data)
    {
        try 
        {
            
$sql "UPDATE ingreso SET 
                        rif          = ?, 
                        nombre        = ?,
                        producto            = ?, 
                        sistema_enc = ?,
                        nro_cupon = ?,
                        fecha_envio = ?,
                        nota = ?
                    WHERE id = ?"
;

            
$this->pdo->prepare($sql)
                 ->
execute(
                array(
                    
$data->__GET('Rif'), 
                    
$data->__GET('Nombre'), 
                    
$data->__GET('Producto'),
                    
$data->__GET('sistema_enc'),
                    
$data->__GET('nro_cupon'),
                    
$data->__GET('fecha_envio'),
                    
$data->__GET('nota'),
                    
$data->__GET('id')
                    
                    )
                            
                );
        }
        
        
        catch (
Exception $e
        {
            die(
$e->getMessage());
        }
    }

    public function 
Registrar(Usuario $data)
    {
        try 
        {
        
$sql "INSERT INTO ingreso (rif,nombre,producto,sistema_enc,nro_cupon,fecha_envio,nota) 
                VALUES (?, ?, ?, ?, ?, ?, ?)"
;

        
$this->pdo->prepare($sql)
             ->
execute(
            array(
                    
$data->__GET('Rif'), 
                    
$data->__GET('Nombre'), 
                    
$data->__GET('Producto'),
                    
$data->__GET('sistema_enc'),
                    
$data->__GET('nro_cupon'),
                    
$data->__GET('fecha_envio'),
                    
$data->__GET('nota'),
                )
            );
        } catch (
Exception $e
        {
            die(
$e->getMessage());
        }
    }

    
}
USUARIO.ENTIDAD.PHP
Código PHP:
<?php
class Usuario
{
    private 
$id;
    private 
$rif;
    private 
$nombre;
    private 
$producto;
    private 
$sistema_enc;
    private 
$nro_cupon;
    private 
$fecha_envio;
    private 
$nota;


    public function 
__GET($k){ return $this->$k; }
    public function 
__SET($k$v){ return $this->$k $v; }
}
Se que posiblemente para algunas personas sea super sencillo, ya he buscado en varios foros sin exito.

Yo deseo que cuando se active el boton guardar tambien en mi tabla "vitacora" se guarde solo los datos rif y nota con la respectiva fecha de cuando se creo dicho reporte.
  #5 (permalink)  
Antiguo 21/12/2016, 22:17
(Desactivado)
 
Fecha de Ingreso: septiembre-2008
Ubicación: Maracay edo Aragua Venezuela
Mensajes: 48
Antigüedad: 15 años, 7 meses
Puntos: 3
Sonrisa Respuesta: Hacer UPDATE en una tabla e INSERT a otra tabla

Solucione utilizando el siguiente codigo:

Código PHP:
public function Actualizar(Usuario $data)
    {
        try 
        {
            
$sql "UPDATE ingreso SET 
                        rif          = ?, 
                        nombre        = ?,
                        producto            = ?, 
                        sistema_enc = ?,
                        nro_cupon = ?,
                        fecha_envio = ?,
                        nota = ?
                    WHERE id = ?"
;
            
$sql2 "INSERT INTO vitacora (rif,nombre,producto,sistema_enc,nro_cupon,fecha_envio,nota,id) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
;
                    

            
$this->pdo->prepare($sql)
                 ->
execute(
                array(
                    
$data->__GET('Rif'), 
                    
$data->__GET('Nombre'), 
                    
$data->__GET('Producto'),
                    
$data->__GET('sistema_enc'),
                    
$data->__GET('nro_cupon'),
                    
$data->__GET('fecha_envio'),
                    
$data->__GET('nota'),
                    
$data->__GET('id')
                    
                    )
                            
                );
            
$this->pdo->prepare($sql2)
                 ->
execute(
                array(
                    
$data->__GET('Rif'), 
                    
$data->__GET('Nombre'), 
                    
$data->__GET('Producto'),
                    
$data->__GET('sistema_enc'),
                    
$data->__GET('nro_cupon'),
                    
$data->__GET('fecha_envio'),
                    
$data->__GET('nota'),
                    
$data->__GET('id')
                    
                    )
                            
                );                
        } 

Etiquetas: mysqli, php+base+de+datos+array
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:58.