estoy tratando de poner las variables con las que haré mí conexión a mí base de datos en un archivo aparte. El archivo es así (he omitido contraseñas y nombres para hacer la pregunta):
connection.php:
define ( 'DB_HOST', 'localhost');
define ( 'DB_USER', '******');
define ( 'DB_PASSWORD', '******');
define ( 'DB_NAME', '******');
Y luego el archivo con el que uso este código es el siguiente.
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);
El problema está en que hace la conexión bien, pero al ejecutar la sentencia de sql falla.
Me gustaría añadir que cuando en vez del include defino las variables como locales funciona. Para hacer la prueba he usado este formulario:
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>
El resultado es este:
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