Send records to several tabalas- mysqli

0

I would like to know how it is sent from a single form to different tables.

for example I have the following code could add an example by sending different records to two tables.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>

<body>

    <?php
$conexion=mysqli_connect("localhost","root","","bdnutriologo") or
    die("Problemas con la conexión");

mysqli_query($conexion,"insert into expediente(id_exp,peso,edad,estatura,fecha_registro,sexo,objetivo) values 
                       ('$_POST[id_exp]','$_POST[peso]','$_POST[edad]','$_POST[estatura]','$_POST[fecha_registro]','$_POST[sexo]','$_POST[objetivo]')")



  or die("Problemas en el select".mysqli_error($conexion));

mysqli_close($conexion);

echo "El paciente fue dado de alta.";
?>
</body>
</html>
    
asked by Digital Renegade 17.04.2018 в 03:31
source

1 answer

2

As such, there are no SQL statements to insert in 2 tables, even though it could be achieved with procedures and cascade depending on the needs.

If you want to insert records in more than one table, you will have to insert each table.

That is:

<?php
$conexion=mysqli_connect("datos conexion");

mysqli_query($conexion, "primer insert tabla 1");
mysqli_query($conexion, "primer insert tabla 2");
mysqli_query($conexion, "primer insert tabla 3");

To ensure the consistency of the data, when these are dependent we can use transactions, to ensure that all queries are executed correctly, or in case something fails to decide what to do, rollback or other action.

/* Deshabilitar autocommit */
mysqli_autocommit($conexion, false);

/* Flag */
$error = false;

if( !mysqli_query($conexion, "consulta 1") ) {
    $error = true;
}
if( !mysqli_query($conexion, "consulta 2") ) {
    $error = true;
}

if($error) {
    /* Revertir los cambios*/
    mysqli_rollback($conexion);

    echo "Algo falló";
    exit;
}

/* Consignar insert */
mysqli_commit($conexion);
    
answered by 17.04.2018 / 13:21
source