updating of records in the PHP database [closed]

0

this part of the code to update records but I get this error:

  

Fatal error: Uncaught Error: Call to undefined function   mysql_fetch_array () in   C: \ xampp \ htdocs \ Handywoman \Truji \ ModifyProcess.php: 17   Stack trace: # 0 {main} thrown in   C: \ xampp \ htdocs \ Aero assistance \ Trek application \ modifyProcess.php on   line 17

I do not know how to solve it

Obviously I already have the form but it does not update me.

<html>

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

<?php 
$id = $_POST['id'];

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


    $query = "SELECT FROM usuario WHERE id = '".$id."'";
    $result = mysqli_query($conexion,$query);

while ($registro = mysql_fetch_array($result)){

echo "
<body>

<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='".$registro['id_Funcionario']."'></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='".$registro['primer_Nombre']."'></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='".$registro['segundo_Nombre']."'></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='".$registro['primer_Apellido']."'></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='".$registro['segundo_Apellido']."'></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='".$registro['cargo_Funcionario']."'></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='".$registro['telefono_Funcionario']."'></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='".$registro['edad_Funcionario']."'></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='".$registro['sexo_Funcionario']."'></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>
";
} 
?>
</body>

</html>
    
asked by Cristian Antonio Trujillo Gris 15.03.2018 в 21:09
source

3 answers

2

Your code does not have only a syntax error when you mix functions mysql* with functions mysqli , but persists in it a serious security hole , the which should be corrected.

In fact, if you pass the query like this:

$query = "SELECT FROM usuario WHERE id = '".$id."'";

Any malicious user could inject you with harmful code not only in the database, but in your entire system.

This bug is corrected in a very simple way, through the use of prepared queries.

I have also taken the opportunity to correct some things. For example, you print a <body> tag each time within while , so you'll have an HTML with a lot of body , which is incorrect.

I have applied certain optimization criteria in the code, such as putting in SELECT only those columns that are going to be used. Sometimes you use SELECT * for convenience, but this is a bad practice, since we are perhaps bringing columns that we will never use.

On the other hand, mysqli has a problem that for me is too uncomfortable and it is that it takes too many laps to get the results when using prepared queries. The values could be obtained in an associative array as you had with get_result , but it turns out that this function is not present in all PHP installations because it is a separate driver. I do not understand why the mysqli designers did that as well. The truth is that here the code is a little more verbose because of that, because you have to link each column explicitly to a variable, which will be used later in the printing of the table . Take note of this, because in the table you should print the values for example: value='".$primerNombre."' and not as you had before: $registro['primer_Nombre'] .

I leave here the modified code. I hope it is useful for you. Keep in mind that here we are correcting several things, and the most important of them is that we are writing a code that is safe against possible attacks that could be quite serious .

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 = ?";

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

    /* 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);

    /* ejecutar la consulta */
    mysqli_stmt_execute($stmt);

    $stmt->store_result();

    /* Bind resultados a variables */
    $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>
    
answered by 15.03.2018 / 22:20
source
1

You have a poorly written query:

$query = "SELECT FROM usuario WHERE id = '".$id."'";

And being wrong, then throw an error and that's why you can not use the function mysql_fetch_array() . The correct thing would be:

$query = "SELECT * FROM usuario WHERE id = '".$id."'";

You can also select the fields of the table you want to show.

    
answered by 15.03.2018 в 21:30
1

The errors I can see are:

  • You needed the '*' that indicates the selection of all the data in your table.
  • When trying to use already obsolete MySQL functions
  • Your code should look like this:

    <html>
    
    <head>
    <title>Datos a actualizar.</title>
    <META name='robot' content='noindex, nofollow'>
    </head>
    
    <?php 
    $id = $_POST['id'];
    
    $conexion =  mysqli_connect("localhost", "root","admin123","database");
    
    
        $query = "SELECT * FROM usuario WHERE id = '".$id."'";
        $result = $conexion->query($query);
    
    
    while ($registro = mysqli_fetch_array($result)){
    
    echo "
    <body>
    
    <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='".$registro['id_Funcionario']."'></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='".$registro['primer_Nombre']."'></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='".$registro['segundo_Nombre']."'></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='".$registro['primer_Apellido']."'></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='".$registro['segundo_Apellido']."'></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='".$registro['cargo_Funcionario']."'></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='".$registro['telefono_Funcionario']."'></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='".$registro['edad_Funcionario']."'></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='".$registro['sexo_Funcionario']."'></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>
    ";
    } 
    ?>
    </body>
    
    </html>
    

    Note: mysql_fetch_array

      

    This extension was declared obsolete in PHP 5.5.0 and deleted in PHP   7.0.0. Instead the MySQLi or PDO_MySQL extensions should be used.

        
    answered by 15.03.2018 в 21:24