dimanche 12 juin 2016

Script is not connecting [duplicate]


I am using JS to register users to my database. The register part of my script worked before. I extended my script with a check. The script checks if the $name that the user has entered is in use. If it is in use the script needs to show echo "Name is in use";.

When I run this script, I get die('Error: ' . mysqli_error($con));.

In my browser I get: Error:.

What is wrong with my script?

function register($name,$email,$password) {

 $errors      = array();
 $messages    = array();

 if( empty($name)=== true || empty($email)=== true || empty($password) === true  ) {
  die("One ore more fields are empty");   
 }
 else
 {    
  $query = mysqli_query($conn, "SELECT * FROM users WHERE name='".$name."'");
  if(mysqli_num_rows($query) > 0) {
   echo "Name is in use";
  }
  else
  {
   $name = trim($name);
   $email = trim($email);
   $password = trim($password);

   $sql = "INSERT INTO users (name, email, password) VALUES('$name','$email',md5('$password'))";
   $result=mysqli_query($sql);

   if($result) {
    echo "Success";
   }

   if (!mysqli_query($conn,$query))
   {
    die('Error: ' . mysqli_error($conn));
   }
  }
 }
}

$conn is defined in another script that already is included:

<?php

define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DBNAME','db');

class Connection {

    private static $_instace;
    private $conn;

    private function __construct() {
        $this->conn = mysqli_connect(HOST, USER, PASS);
        mysqli_select_db(DBNAME);
    }

    public static function getconnect() {
        if (!self::$_instace) {
            self::$_instace = new Connection();
        }
        return self::$_instace;
    }

    public function mysqli_execute($sql) {
        $sql = ltrim($sql);
        $arr_get = array();
    }

    private function __close() {    
    }
}
?>

Aucun commentaire:

Enregistrer un commentaire