Perform update of records in Php [closed]

1

    Start update     

<div align="center">
    <table border="0" width="600" style="font-family: Verdana; font-size: 8pt" id="table1">
        <tr>
            <td colspan="2"><h3 align="center">Actualizacion de datos</h3></td>
        </tr>
        <form method="POST" action="modificarProceso.php">
            <tr>
                <td width="50%">&nbsp;</td>
                <td width="50%">&nbsp;</td>
            </tr>
            <tr>
                <td width="50%">
                    <p align="center"><b>ID del funcionario a actualizar: </b></td>
                        <td width="50%">
                            <p align="center"><input type="text" name="id" size="20"></td>
                            </tr>
                            <tr>
                                <td width="50%">&nbsp;</td>
                                <td width="50%">&nbsp;</td>
                            </tr>
                            <tr>
                                <td width="100%" colspan="2">
                                    <p align="center">
                                        <input type="submit" value="Iniciar actualización" name="btn-actulizar"></td>
                                    </tr>
                                </form>
                            </table>
                            <input type="button" value="Volver de donde viniste!" onclick="history.back(-1)" />
                        </div>

                    </body>

                    </html> 

modifyProcess.php

<html>

<head>
    <title>Datos a actualizar.</title>
    <META name='robot' content='noindex, nofollow'>
</head>

<?php 
echo "<body>";
$id = $_POST['id'];

$conexion =  mysqli_connect("localhost", "root","admin123","database");

$query = "  SELECT
                    id_Funcionario,
                    primer_Nombre,
                    segundo_Nombre,
                    primer_Apellido,
                    segundo_Apellido,
                    cargo_Funcionario,
                    telefono_Funcionario,
                    edad_Funcionario,
                    sexo_Funcionario
            FROM usuario
            WHERE id = ?";

/*Prepara la setencia SQl para su ejecucion*/
if ($stmt = mysqli_prepare($conexion, $query)) {

    /*Agrega variables a una sentencia preparada como parametros*/
    /* ligar parámetros para marcadores */
    /*IMPORTANTE: Si id no es numérico en la base de datos, cambia la i por una s*/
    mysqli_stmt_bind_param($stmt, "i", $id);

    /* Ejecuta una consulta preparada */
    mysqli_stmt_execute($stmt);

    /*Se transfiere los  resultados de la ultima consulta*/
    $stmt->store_result();

    /* Vincula las variables a una setencia preparada para el almacenamineto de resultados */
    $stmt->bind_result($idFuncionario, $primerNombre, $segundoNombre, $primerApellido, $segundoApellido, $cargo, $tel, $edad, $sexo);

    while ($stmt->fetch()) {
        echo "

    <div align='center'>
        <table border='0' width='600' style='font-family: Verdana; font-size: 8pt' id='table1'>
            <tr>
                <td colspan='2'><h3 align='center'>Actualice los datos que considere</h3></td>
            </tr>
            <tr>
                <td colspan='2'>En los campos del formulario puede ver los valores actuales,
                si no se cambian los valores se mantienen iguales.</td>
            </tr>
            <form method='POST' action='actualiza.php'>
            <tr>
                <td width='50%'>&nbsp;</td>
                <td width='50%'>&nbsp;</td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Cedula: </b></td>
                <td width='50%'><p align='center'><input type='text' name='id_funcinario' size='20' value='".$idFuncionario."'></td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Primer Nombre :</b></td>
                <td width='50%'><p align='center'><input type='text' name='primer_Nombre' size='20' value='".$primerNombre."'></td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Segundo Nombre :</b></td>
                <td width='50%'><p align='center'><input type='text' name='segundo_Nombre' size='20' value='".$segundoNombre."'></td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Primer Apellido :</b></td>
                <td width='50%'><p align='center'><input type='text' name='primer_Apellido' size='20' value='".$primerApellido."'></td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Segundo Apellido :</b></td>
                <td width='50%'><p align='center'><input type='text' name='segundo_Apellido' size='20' value='".$segundoApellido."'></td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Cargo Funcionario :</b></td>
                <td width='50%'><p align='center'><input type='text' name='cargo_Funcionario' size='20' value='".$cargo."'></td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Telefono Funcionario :</b></td>
                <td width='50%'><p align='center'><input type='text' name='telefono_Funcionario' size='20' value='".$tel."'></td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Edad Funcionario :</b></td>
                <td width='50%'><p align='center'><input type='text' name='edad_Funcionario' size='20' value='".$edad."'></td>
            </tr>
            <tr>
                <td width='50%'><p align='center'><b>Sexo Funcionario :</b></td>
                <td width='50%'><p align='center'><input type='text' name='sexo_Funcionario' size='20' value='".$sexo."'></td>
            </tr>
            <tr>
                <td width='50%'>&nbsp;</td>
                <td width='50%'>&nbsp;</td>
            </tr>
            <input type='hidden' name='id' value='$id'>
            <tr>
                <td width='100%' colspan='2'>
                <p align='center'>
                <input type='submit' value='Actualizar datos' name='B1'></td>
            </tr>
            </form>
        </table>
    </div>
    ";
    } 

}else{
    echo "Hubo un error en la consulta";
}
?>
    </body>
    </html>

actualiza.php
<head>
<title>Actualizacion completada.</title>
<META name='robot' content='noindex, nofollow'>

<?php

// We update according to the id we receive

$id = $_POST['id'];

//$id_Funcionario = $_POST['id_Funcionario'];
$primer_Nombre = $_POST['primer_Nombre'];
$segundo_Nombre = $_POST['segundo_Nombre'];
$primer_Apellido = $_POST['primer_Apellido'];
$segundo_Apellido = $_POST['segundo_Apellido'];
$cargo_Funcionario = $_POST['cargo_Funcionario'];
$telefono_Funcionario = $_POST['telefono_Funcionario'];
$edad_Funcionario = $_POST['edad_Funcionario'];
$sexo_Funcionario = $_POST['sexo_Funcionario'];
$fecha = date("d-m-Y");

$conexion =  mysqli_connect("localhost", "root","admin123","database");


$sSQL="UPDATE usuario SET 
                            primer_Nombre=?, 
                            segundo_Nombre=?, 
                            primer_Apellido=?, 
                            segundo_Apellido=?, 
                            cargo_Funcionario=?, 
                            telefono_Funcionario=?, 
                            edad_Funcionario=?, 
                            sexo_Funcionario=?

        WHERE id_Funcionario=?";  //aquí habría que filtrar por algún valor

if ($stmt = mysqli_prepare($conexion, $sSQL)) {

    /*Agrega variables a una sentencia preparada como parametros*/
    /* ligar parámetros para marcadores */
    /*IMPORTANTE: Si id no es numérico en la base de datos, cambia la i por una s*/
    mysqli_stmt_bind_param($stmt, "sssssiisi", $id_Funcionario,$primer_Nombre,$segundo_Nombre,$primer_Apellido,$segundo_Apellido,$cargo_Funcionario,$telefono_Funcionario,$edad_Funcionario,$sexo_Funcionario);

    /* Ejecuta una consulta preparada */
    mysqli_stmt_execute($stmt);

    if (mysqli_stmt_error($stmt)){
        echo mysqli_stmt_errno($stmt);      
    }else{
        echo "Se actualizaron: ".mysqli_affected_rows($conexion)." Prueba temporal: ".mysqli_error($conexion);    
    }
    mysqli_stmt_close ($stmt);
}else{

    echo "Hubo un error en la actual";
    echo '<a href="javascript:history.go(-1);">Atras</a';
}       
?>

    
asked by Cristian Antonio Trujillo Gris 16.03.2018 в 14:40
source

1 answer

1

This code is for the file modificarProceso.php .

The other code already has an answer in the other question where it has been explained in more detail how the prepared queries work.

  

VERY IMPORTANT NOTE: You have to specify based on what criteria you   will do the UPDATE using a WHERE , otherwise, they will be updated   all the data in the table with those values.

That is, your query would lack a filter:

    $sSQL="UPDATE usuario SET 
                                id_Funcionario=?, 
                                primer_Nombre=?, 
                                segundo_Nombre=?, 
                                primer_Apellido=?, 
                                segundo_Apellido=?, 
                                cargo_Funcionario=?, 
                                telefono_Funcionario=?, 
                                edad_Funcionario=?, 
                                sexo_Funcionario=?
            WHERE id=1";  //aquí habría que filtrar por algún valor

update.php

<head>
    <title>Actualizacion completada.</title>
    <META name='robot' content='noindex, nofollow'>
</head>

<body>

    <?php
// Actualizamos en funcion del id que recibimos

    $id = $_POST['id'];

    $primer_Nombre = $_POST['primer_Nombre'];
    $segundo_Nombre = $_POST['segundo_Nombre'];
    $primer_Apellido = $_POST['primer_Apellido'];
    $segundo_Apellido = $_POST['segundo_Apellido'];
    $cargo_Funcionario = $_POST['cargo_Funcionario'];
    $telefono_Funcionario = $_POST['telefono_Funcionario'];
    $edad_Funcionario = $_POST['edad_Funcionario'];
    $sexo_Funcionario = $_POST['sexo_Funcionario'];
    $fecha = date("d-m-Y");

    $conexion =  mysqli_connect("localhost", "root","admin123","database");


    $sSQL="UPDATE usuario SET 
                                primer_Nombre=?, 
                                segundo_Nombre=?, 
                                primer_Apellido=?, 
                                segundo_Apellido=?, 
                                cargo_Funcionario=?, 
                                telefono_Funcionario=?, 
                                edad_Funcionario=?, 
                                sexo_Funcionario=?

            WHERE id_Funcionario=?";

    if ($stmt = mysqli_prepare($conexion, $sSQL)) {

        /*Agrega variables a una sentencia preparada como parametros*/
        /* ligar parámetros para marcadores */
        /*IMPORTANTE: Si id no es numérico en la base de datos, cambia la i por una s*/
        mysqli_stmt_bind_param($stmt, "sssssiisi", $primer_Nombre,$segundo_Nombre,$primer_Apellido,$segundo_Apellido,$cargo_Funcionario,$telefono_Funcionario,$edad_Funcionario,$sexo_Funcionario,$id);

        /* Ejecuta una consulta preparada */
        mysqli_stmt_execute($stmt);

        if (mysqli_stmt_error($stmt)){
            echo mysqli_stmt_errno($stmt);      
        }else{
            echo "Se actualizaron: ".mysqli_affected_rows($conexion);       
        }
        mysqli_stmt_close ($stmt);
    }else{

        echo "Hubo un error preparando la consulta";
    }       
    
answered by 16.03.2018 / 15:05
source