I have a problem with creating a user record, what I am trying to do is that from the inputs that I create in html, the information is stored in php, create the variable with the information obtained from the input and use that variable to create a new user in my Database with SQL (you already know the typical).
But when registering it says this error
Fatal error: Uncaught Error: Call to a member function prepare() on null on line 15.
I already checked but it seems that I am doing something wrong, I hope I have given you all the necessary information below the code:
<?php
session_start();
// Datos para conectar a la base de datos.
$nombreServidor = "*********";
$nombreUsuario = "******";
$passwordBaseDeDatos = "*********";
$nombreBaseDeDatos = "******";
// Crear conexión con la base de datos.
$conn = new mysqli($nombreServidor, $nombreUsuario, $passwordBaseDeDatos, $nombreBaseDeDatos);
// registrar usuario
$stmt = $con->prepare("INSERT INTO usuarios (correo,password,nombre,apellido_p,apellido_m,colonia,calle,numero,CP,telefono) VALUES (:email,:contra,:nombre,:Apaterno,:Amaterno,:colonia,:calle,:numero,:CP,:tel)");
//$stmt->bind_param(array($email,$contra,$nombre,$Apaterno,$Amaterno,$colonia,$calle,$numero,$CP,$tel));
$stmt->bindParam(':email', $email);
$stmt->bindParam(':contra', $contra);
$stmt->bindParam(':nombre', $nombre);
$stmt->bindParam(':Apaterno', $Apaterno);
$stmt->bindParam(':contra', $Amaterno);
$stmt->bindParam(':Amaterno', $colonia);
$stmt->bindParam(':calle', $calle);
$stmt->bindParam(':numero', $numero);
$stmt->bindParam(':CP', $CP);
$stmt->bindParam(':tel', $tel);
$email = $_POST['email'];
$contra = $_POST['psw'];
$nombre = $_POST['name'];
$Apaterno = $_POST['apaterno'];
$Amaterno = $_POST['amaterno'];
$colonia = $_POST['colonia'];
$calle = $_POST['calle'];
$numero = $_POST['numero'];
$CP = $_POST['codigop'];
$tel = $_POST['telefono'];
$stmt->execute();
/*$sql = "INSERT INTO usuarios (correo,password,nombre,apellido_p,apellido_m,colonia,calle,numero,CP,telefono) VALUES ('$_POST[email]','$_POST[psw]','$_POST[name]','$_POST[apaterno]','$_POST[amaterno]','$_POST[colonia]','$_POST[calle]','$_POST[numero]','$_POST[codigop]','$_POST[telefono]')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
*/
?>
If you are building your connection through a mysqli instance , then:
bind_param
instead ofbindParam
it's from the PDO classbind_param
receives,
each of the variablesprepare
using mysqli , we will use the symbol?
instead of the parameter marker as markers.Try your code like this: