Use variables defined in another PHP file

0

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
    
asked by Mikel Molinuevo 03.06.2018 в 15:45
source

1 answer

1

The problem is here: $sql = mysqli_prepare($conn,"INSERT INTO Usuario VALUES('$mail','$usrName','$encPass','$date','$name','$apUno','$apDos')"); , being a prepared statement should be something like this:

$sql = mysqli_prepare($conn,"INSERT INTO Usuario VALUES(?,?,?,?,?,?,?)");
mysqli_stmt_bind_param($sql, "sssssss", $mail, $usrName, $passUsr, $date, $name, $apUno, $apDos);

The sssssss correspond to the type of data you will enter in the sentence.

You can see more about prepared sentences here or here

EDIT: Not only is the error in that statement, in the following (those that are within the if ) there is also this failure. If you go to the docs of the prepared sentences you will find information to be able to apply the correctly prepared sentences.

    
answered by 03.06.2018 / 15:53
source