dimanche 26 juin 2016

mysqli_query not adding user

I'm trying to create a log in feature using PHP and MySQL.

I'm pretty new to back end coding so I'm having some issues. I've checked all other threads on here and nothing has worked.

I am using localhost and I've managed to connect the database I created.

I used this query to create the database and table:

CREATE DATABASE `dbtest` ;
CREATE TABLE `dbtest`.`users` (
`user_id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 35 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL ,
UNIQUE (`email`)
) ENGINE = MYISAM ;

Here is my file: dbconnect.php connecting to the database:

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "root";



// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(!mysqli_select_db($conn, "dbtest"))
{
	die('oops database selection problem ! --> '.mysqli_error());
}
?>

and my registration page:

<?php
session_start();
if(!empty($_SESSION['user']))
{
	header("Location: home.php");
}
include_once 'dbconnect.php';

if(isset($_POST['btn-signup']))
{
	$uname = mysqli_real_escape_string($conn, $_POST['uname']);
	$email = mysqli_real_escape_string($conn, $_POST['email']);
	$upass = md5(mysqli_real_escape_string($conn, $_POST['pass']));
	
	$uname = trim($uname);
	$email = trim($email);
	$upass = trim($upass);
	
	// email exist or not
	$query = "SELECT user_email FROM users WHERE user_email='$email'";
	$result = mysqli_query($conn, $query);
	
	$count = mysqli_num_rows($result); // if email not found then register
	
	if($count == 0){
		
		if(mysqli_query($conn, "INSERT INTO users(user_name,user_email,user_pass) VALUES('$uname','$email','$upass')"))
		{
			?>
			
			<?php
            echo ("<p style='color:green; text-align:center; font-size:30px'>Successful!: </p>");
		}
		else
		{
			?>
			
			<?php
            echo ("<p style='color:red; text-align:center; font-size:30px'>Error While Registering you : </p>");
		}		
	}
	else{
			?>
			<script>alert(' ...');</script>
			<?php
        echo ("<p style='color:red; text-align:center; font-size:30px'>This Email address is already taken: </p>");
	}
	
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration System</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />

</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="uname" placeholder="User Name" required /></td>
</tr>
<tr>
<td><input type="email" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Sign Me Up</button></td>
</tr>
<tr>
<td><a href="index.php">Sign In Here</a></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

When I try to register a new user, the SQL query gives me the error "Error while registering you" on line 39 of register.php.

Can anyone see why this is? I've been stuck for days now.

I've tried adding $conn, as the first parameter of the queries (similar to line 13 of dbconnect.php as I read somewhere that it should fix it. It didn't so I've removed them for now.

Aucun commentaire:

Enregistrer un commentaire