Problems when doing an INSERT with php

0

I have a registration form and to enter the data in the database I have to do INSERT in different tables but in the same query. The php code that I have is the following:

<?php

//OBTENGO LOS DATOS DEL FORMULARIO
$usuario = $_POST["usuario"];
$mail = $_POST["mail"];
$password = $_POST["password"];
$passwordEncriptada = password_hash($password, PASSWORD_DEFAULT, array("cost" => 12));

require("datos_conexion.php");

$conexion = mysqli_connect($db_host, $db_user, $db_pass);

if (mysqli_connect_errno()) {
    echo "Fallo al conectar con la base de datos";
    exit();
}

mysqli_select_db($conexion, $db_nombre) or die("No se encuentra la base de datos");
mysqli_set_charset($conexion, "utf8");

//HAGO LOS INSERT
$registro = "INSERT INTO registro (Reg_Usu, Reg_Mail, 
Reg_Pass1) VALUES ('$usuario', '$mail', '$passwordEncriptada'); "
    . "INSERT INTO contacto (Con_Usu, Con_Mail) VALUES ('$usuario', '$mail'); "
    . "INSERT INTO datos_empresa (Emp_Usu) VALUES ('$usuario'); "
    . "INSERT INTO direcciones (Dir_Usu) VALUES ('$usuario'); "
    . "INSERT INTO usu_datos (Usu_Usu) VALUES ('$usuario'); ";


$resultados = mysqli_query($conexion, $registro);

if($resultados == false){

    echo"La consulta o los datos estan mal!";
    echo "<br><br>" . $registro;
}else{
    session_start();
    $_SESSION["usuario"] = $_POST["usuario"];
    header("location:../login/index.php");
}

mysqli_close($conexion);
?>

When executing the code, the La consulta o los datos estan mal! message I get from validating the results with the mysqli_query($conexion, $registro) function is printed on the screen. I have seen that the INSERT syntax is correct and the data entered are correct and the funny thing is that when I do it from phpMyAdmin (copying and pasting the same code and using the same values obtained from the variables)

INSERT INTO registro (Reg_Usu, Reg_Mail, Reg_Pass1) VALUES ('usuario', '[email protected]', '$2y$12$1vHoFlWYZlsrO1nVSx1X/eIVwc68dv6MorZxQMO9OMS'); 
    INSERT INTO contacto (Con_Usu, Con_Mail) VALUES ('usuario', '[email protected]'); 
    INSERT INTO datos_empresa (Emp_Usu) VALUES ('usuario'); 
    INSERT INTO direcciones (Dir_Usu) VALUES ('usuario'); 
    INSERT INTO usu_datos (Usu_Usu) VALUES ('$usuario'); 

I insert everything correctly, but in doing so from the php code no. Would someone know how to find a solution?

    
asked by gmarsi 10.04.2017 в 04:05
source

1 answer

1

A possible solution would be to use mysqli_multi_query (connection, query) to execute multiple queries by sending you as parameter the Connection Object and the query that we want to execute.

Los datos dentro de la consulta debe estar correctamente escapados mysqli_real_escape_string

$query= "INSERT INTO table1 (campo1) VALUES (value1);
         INSERT INTO table2 (campo1,campo2) VALUES (value1, value2);
         INSERT INTO tabla3 (campo1) VALUES (value1);"
$result= mysqli_multi_query($conexion, $query);
if ($result) {
    echo "CONSULTAS EJECUTADAS CORRECTAMENTE";
}
    
answered by 10.04.2017 / 04:27
source