0

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>
Newbie
  • 1
  • 2
  • 1
    You can't mix mysqli functions and the deprecated mysql functions like `mysql_select_db`. – cteski Jun 15 '17 at 14:02
  • Also `functions` have to be called!!!! – RiggsFolly Jun 15 '17 at 14:02
  • cteski: $sql = so new mysqli ('localhost', 'root', '', "mydb"); is OK? – Newbie Jun 15 '17 at 14:05
  • [The MYSQLI_ Database extension manual start here](http://php.net/manual/en/book.mysqli.php) and so should you – RiggsFolly Jun 15 '17 at 14:05
  • RiggsFolly: Yeah, I know, I called them in an other file. The function calling works, but I get the else way, which means error message. – Newbie Jun 15 '17 at 14:06
  • Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. – RiggsFolly Jun 15 '17 at 14:06
  • Thanks for your help! – Newbie Jun 15 '17 at 14:20

0 Answers0