Example #1
0
 public function insert()
 {
     if (!is_null($this->id)) {
         trigger_error("User::insert(): Attempt to insert a user object that already has its ID property set to {$this->id}.", E_USER_ERROR);
     }
     // Set Values
     $this->password = Password::hash($this->password);
     // Validation
     if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
         self::$errorCode = "ERR_INV_EMAIL";
         return false;
     } else {
         if (strlen($this->name) < MINIMUM_NAME_LENGTH || preg_match("/[^a-zA-Z'-]/", $this->name)) {
             self::$errorCode = "ERR_INV_NAME";
             return false;
         } else {
             if (strlen($this->phone) != 10 || preg_match("/[^[0-9]{10}]/", $this->phone)) {
                 self::$errorCode = "ERR_INV_PHONE";
                 return false;
             }
         }
     }
     $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
     $sql = "INSERT INTO " . TABLENAME_USERS . " ( name, birthday, age, gender,college, password, phone, email, icon_link, aboutMe, verification ) VALUES ( :name, :birthday, :age, :gender, :college, :password, :phone, :email, :icon_link, :aboutMe, :verification )";
     $st = $conn->prepare($sql);
     $st->bindValue(":name", $this->name, PDO::PARAM_STR);
     $st->bindValue(":birthday", $this->birthday, PDO::PARAM_STR);
     $st->bindValue(":age", $this->age, PDO::PARAM_INT);
     $st->bindValue(":gender", $this->gender, PDO::PARAM_STR);
     $st->bindValue(":college", $this->college, PDO::PARAM_STR);
     $st->bindValue(":password", $this->password, PDO::PARAM_STR);
     $st->bindValue(":email", $this->email, PDO::PARAM_STR);
     $st->bindValue(":phone", $this->phone, PDO::PARAM_STR);
     $st->bindValue(":icon_link", $this->icon_link, PDO::PARAM_STR);
     $st->bindValue(":aboutMe", $this->aboutMe, PDO::PARAM_STR);
     $st->bindValue(":verification", $this->verification, PDO::PARAM_STR);
     //	print_r( $st );
     $result = $st->execute();
     $this->id = $conn->lastInsertId();
     $conn = null;
     if (!$result) {
         self::$errorMessage = "User::insert: Insertion Failed, PDO::errorInfo(): " . $st->errorCode() . ": " . $st->errorInfo()[2];
         self::$errorCode = $st->errorCode();
         //echo $errorMessage;
         return false;
     } else {
         self::$successMessage = "User::insert: User successfully inserted with id " . $this->id;
         return true;
     }
 }