Jump to content

Toomas_99

New Members
  • Posts

    7
  • Joined

  • Last visited

Toomas_99's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. isnt this $defaults = [ 'first_name' => null, 'last_name' => null, 'username' => null, 'password' => null, /* -------------- */ ]; # Any missing elements get the default value. $_post = array_merge( $defaults, array_intersect_key( $_POST, $defaults ) ); basically same thing like this without html escape. <input type="text" name="username" value="<?= isset($_POST['username']) ? html_escape($_POST['username']): '' ?>"> So now when shomeone should remove (when editing template) <?= isset($_POST['username']) ? html_escape($_POST['username']): '' ?> and replace this line with <?=$_POST['username'];?>
  2. Because this code is just testing purpose. I have not write any error checking and xss preventing stuff. Like if ( empty( $_post[ 'username' ] ) ) { $errors[] = 'Please enter your username.'; }
  3. What is best way to avoid Undefined index? Is this good approach? <?php $defaults = [ 'first_name' => null, 'last_name' => null, 'username' => null, 'password' => null, /* -------------- */ ]; # Any missing elements get the default value. $_post = array_merge( $defaults, array_intersect_key( $_POST, $defaults ) ); if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) { var_dump( $_post ); } ?> <!DOCTYPE html> <html> <body> <form action="" method="post"> First name:<br> <input type="text" name="first_name" value="<?=$_post['first_name'];?>"> <br> Last name:<br> <input type="text" name="last_name" value="<?=$_post['last_name'];?>"> <br> Username:<br> <input type="text" name="username" value="<?=$_post['username'];?>"> <br> Password:<br> <input type="password" name="password" value="<?=$_post['password'];?>"> <br><br> <input type="submit" name="submit_form" value="Submit"> </form> </body> </html>
  4. What will this line mean? if ( $select->fetchColumn() ) { return true; } I know that this will check if row count is bigger than 0 if ( $select->fetchColumn() > 0 ) { return true; } And this - If row count is equal to 1 if ( $select->fetchColumn() == 1 ) { return true; }
  5. 1. $row = $select->fetch( PDO::FETCH_ASSOC ); 2. All of them?
  6. 1 - Should i use $row = $select->fetch( PDO::FETCH_OBJ ); when working with oop? Example public function usernameToId( $username ) { # Create and run query. $query = 'SELECT id FROM user WHERE username = :username'; $select = $this->db->prepare( $query ); $select->bindParam( ':username', $username, PDO::PARAM_STR ); $select->execute(); # Retrieve the row( will return an empty array if no records match ). $row = $select->fetch( PDO::FETCH_OBJ ); # if ( $row ) { return $row->id; } # No rows matched. return null; } 2 . What is corrcect method to see if username exists? public function usernameExists( $username ) { # Create and run query. $query = 'SELECT COUNT(id) FROM user WHERE username = :username'; $select = $this->db->prepare( $query ); $select->bindParam( ':username', $username, PDO::PARAM_STR ); $select->execute(); # if ( $select->fetchColumn() > 0 ) { return true; } # No rows matched. return false; } public function usernameExists( $username ) { # Create and run query. $query = 'SELECT COUNT(id) FROM user WHERE username = :username'; $select = $this->db->prepare( $query ); $select->bindParam( ':username', $username, PDO::PARAM_STR ); $select->execute(); # if ( $select->fetchColumn() ) { return true; } # No rows matched. return false; } public function usernameExists( $username ) { # Create and run query. $query = 'SELECT COUNT(id) FROM user WHERE username = :username'; $select = $this->db->prepare( $query ); $select->bindParam( ':username', $username, PDO::PARAM_STR ); $select->execute(); # if ( $select->fetchColumn() == 1 ) { return true; } # No rows matched. return false; }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.