Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] PHP OO Registro en la base de datos

Estas en el tema de Registro en la base de datos en el foro de PHP en Foros del Web. Hola a todos, he tomado un host nuevo y tiene instalado la version de php7. He realizado la conexion a la base de datos perfectamente ...
  #1 (permalink)  
Antiguo 14/06/2020, 06:50
 
Fecha de Ingreso: junio-2005
Ubicación: Soy Latinoamericano pero
Mensajes: 95
Antigüedad: 18 años, 10 meses
Puntos: 0
Registro en la base de datos

Hola a todos,
he tomado un host nuevo y tiene instalado la version de php7.
He realizado la conexion a la base de datos perfectamente pero no puedo grabar ningun dato en DB desde el formulario de registro podrian ayudarme para saber cual es mi error? este es el codigo:

I.-) Registro.php (form)

<?php

include "code_register.php";
?>
<!doctype html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->

<!-- Register -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1,
maximum-scale=1, minimun-scale=1">
<title>Register</title>
<link href="css/login.css" rel="stylesheet" type="text/css">
</head>

<body>
<div class="container-all">
<div class="ctn-form">
<img src="images/logo-Lt-2.png" alt="" class="logo">
<h1 class="title">Sign in</h1>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">

<label for="">Username</label>
<input type="text" name="username">
<span class="msg-error"><?php echo $username_err; ?></span>
<label for="">Email</label>
<input type="text" name="email">
<span class="msg-error"><?php echo $email_err; ?></span>
<label for="">Password</label>
<input type="password" name="password">
<span class="msg-error"><?php echo $password_err; ?></span>
<input type="submit" value="Sing In">

</form>

<span class="text-footer">Are you Registered?
<a href="Login.php">Login</a>
</span>

</div>
</div>
</body>
</html>

2.-) code_register.php

<?php

//incluir archivo de conexion a la base de datos
require_once "conexion.php";

//definir variables e inicializar con valores vacios
$username = $email = $password = "";
$username_err = $email_err = $password_err = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){

//VALIDANDO INPUT DE NOMBRE DE USUARIO
if(empty(trim($_POST["username"]))){
$username_err = "Please enter a username";
}else{
//prepara una declaracion de seleccion
$sql = "SELECT id FROM usuarios WHERE usuario =?";

if($stmt = mysqli_prepare($link, $sql)) {
mysqli_stmt_bind_param($stmt, "S", $param_username);

$param_username = trim($_POST["username"]);

if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);

if(mysqli_stmt_num_rows($stmt) == 1){
$username_err = "This username is already registered";
}else{
$username = trim($_POST["username"]);
}
}

}

}
//VALIDANDO INPUT DE NOMBRE DE EMAIL
if(empty(trim($_POST["email"]))){
$email_err = "Please enter an email";
}else{
//prepara una declaracion de seleccion
$sql = "SELECT id FROM usuarios WHERE email =?";

if($stmt = mysqli_prepare($link, $sql)) {
mysqli_stmt_bind_param($stmt, "S", $param_email);

$param_email = trim($_POST["email"]);

if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);

if(mysqli_stmt_num_rows($stmt) == 1){
$email_err = "This email is already registered";
}else{
$email = trim($_POST["email"]);
}
}
}
}


//VALIDANDO PASSWORD
if(empty(trim($_POST["password"]))){
$password_err = "Please enter a password";
}elseif(strlen(trim($_POST["password"])) < 4){
$password_err = "The password must have at least 4 characters";
}else{
$password = trim($_POST["password"]);
}




//COMPROBANDO LOS ERRORES DE ENTRADA ANTES DE INSERTAR LOS DATOS EN LA DB
if(empty($username_err) && empty($email_err) && empty($password_err)){

$sql = "INSERT INTO usuarios (usuario, email, pass) VALUES (?, ?, ?)";


if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "SSS", $param_username, $param_email, $param_password);

//ESTABLECIENDO PARAMETROS
$param_username = $username;
$param_email = $email;
$param_password = password_hash($password, PASSWORD_DEFAULT); //encriptando password


if(mysqli_stmt_execute($stmt)){
header("location: Login.php");
}else{
echo "Something was wrong, try again later";
}
}
}

mysqli_close($link);

}

?>

Etiquetas: css, dato, post, registro
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 10:02.