I am trying to put the variables with which I will make my connection to my database in a separate file. The file is like this (I have omitted passwords and names to ask the question):
connection.php:
define ( 'DB_HOST', 'localhost');
define ( 'DB_USER', '******');
define ( 'DB_PASSWORD', '******');
define ( 'DB_NAME', '******');
And then the file I use this code with is the following.
registroUsuario.php:
require_once __DIR__ . '/connection.php';
//creacion de la conexion
$conn = mysqli_connect(DB_HOST, DB_USER,DB_PASSWORD) or die("Fallo en la conexión");
$db = mysqli_select_db($conn,DB_NAME) or die("No se selecciona la DB");
//cogiendo variables
$mail = $_POST['mail'];
$usrName = $_POST['usuario'];
$passUsr = $_POST['pass'];
$date = $_POST['fecha'];
$name = $_POST['nombre'];
$apUno = $_POST['apellidoUno'];
$apDos = $_POST['apellidoDos'];
$farmOrClient = $_POST['farmOcliente'];
$nssOrLicence = $_POST['nssOrLicence'];
//password encrypting
$encPass = password_hash($passUsr,PASSWORD_DEFAULT);
//sentencia sql y ejecución de esta
$sql = mysqli_prepare($conn,"INSERT INTO Usuario VALUES('$mail','$usrName','$encPass','$date','$name','$apUno','$apDos')");
mysqli_stmt_bind_param($sql, "s", $mail, $usrName, $passUsr, $date, $name, $apUno, $apDos);
mysqli_stmt_execute($sql);
echo 'Bat';
if($farmOrClient == "farm"){
$sql = mysqli_prepare($conn,"INSERT INTO Duenio VALUES('$mail','$nssOrLicence')");
mysqli_stmt_bind_param($sql, "s", $mail, $nssOrLicence);
mysqli_stmt_execute($sql) or die('Txarto');
echo('Ondo');
}else if($farmOrClient == "client"){
$sql = mysqli_prepare($conn,"INSERT INTO Paciente VALUES('$mail','$nssOrLicence')");
mysqli_stmt_bind_param($sql, "s", $mail, $nssOrLicence);
mysqli_stmt_execute($sql) or die('Txarto');
echo('Ondo');
}
//cerrando la conexíon
mysqli_close($conn);
The problem is that it makes the connection fine, but when executing the sql statement it fails.
I would like to add that when instead of the include I define the variables as local works. To make the test I used this form:
pruebaRegistro.html:
<html>
<head>
<title>Prueba</title>
</head>
<body>
<form action="registroUsuario.php" method="post">
<div>
<label for="mail">E-mail:</label>
<input type="text" id="mail" name="mail"/>
</div>
<div>
<label for="usuario">Usuario:</label>
<input type="text" id="usuario" name="usuario"/>
</div>
<div>
<label for="pass">Contraseña:</label>
<input type="text" id="pass" name="pass"/>
</div>
<div>
<label for="fecha">Fecha:</label>
<input type="text" id="fecha" name="fecha"/>
</div>
<div>
<label for="nombre">Nombre:</label>
<input type="text" id="nombre" name="nombre"/>
</div>
<div>
<label for="apellidoUno">Primer Apellido:</label>
<input type="text" id="apellido1" name="apellidoUno"/>
</div>
<div>
<label for="apellidoDos">Segundo Apellido:</label>
<input type="text" id="apellido2" name="apellidoDos"/>
</div>
<div>
<label for="farmOcliente">Cliente o keloke:</label>
<input type="text" id="farmOcliente" name="farmOcliente"/>
</div>
<div>
<label for="nssOrLicence">Mete tu shit:</label>
<input type="text" id="nssOrLicence" name="nssOrLicence"/>
</div>
<div class="button">
<button type="submit">Send your message</button>
</div>
</form>
</body>
</html>
The result is this:
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /storage/ssd5/279/3001279/public_html/PHP/registroUsuario.php on line 28
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /storage/ssd5/279/3001279/public_html/PHP/registroUsuario.php on line 30
Bat
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /storage/ssd5/279/3001279/public_html/PHP/registroUsuario.php on line 35
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /storage/ssd5/279/3001279/public_html/PHP/registroUsuario.php on line 36
Txarto