I'd like to make a simple register system by PHP and MySQL but the username, password and emails can't get in the database. And I can't even check values in DB, weather do they already exist. I get my error messages. What's the problem? Please HELP!
enter code here
<i>
<?php
function registration($user_name, $email, $password) {
//true value or error message
//connection set up
$sql = new mysqli ('localhost', 'root', '');
mysql_select_db("mydb");
if (mysqli_connect_error()) {
print 'Error in connection.';
die("Database connection failed: " . mysqli_connect_error());
}
else {
print 'Connect is OK.';
return $sql;
}
//check username is uniqe or not?
$sql = $connection->query("SELECT * FROM user
WHERE user_name='".$user_name."'");
if (!sql) {
throw new Exception ('Cant query. Sorry');
}
if ($sql->num_rows>0) {
throw new Exception ('The username already exists. Choose an other one!');
}
// if it's OK put in the db else error message
$sql = $connection->query("INSERT INTO user VALUES
('".$user_name."',
shal('".$password."'), '".$email."')");
if (!$sql) {
throw new Exception ('We could not register! Try later!');
}
return true;
}
?>
</i>