1

I'm trying to implement the function where it will check whether if the user already exists in my database be for it will insert all the registration data but it doesn't seem to work =( could someone please help me identify where the error is. really appreciate all the answer in advance.

<?php

require '../ppuyakul/php/db_conn.php';


$message = '';

//Prepare date
$DOB = date("Y-m-d", strtotime( $_POST['year'].'-'. $_POST['month'].'-'. $_POST['day']));
$accessType = "0";

//Check enpty field
if(!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['fullname']) && !empty($_POST['username']) && !empty($_POST['password_confirmation']) && !empty($_POST['gender']) && !empty($_POST['country']) && !empty($_POST['state']) && !empty($_POST['city']) && !empty($_POST['day']) && !empty($_POST['month']) && !empty($_POST['year'])):

  // Enter the new user in the database
  $sql = "INSERT INTO assignment2 (fullname, username, email, password, gender, country, state, city, DOB, type) VALUES (:fullname, :username, :email, :password, :gender, :country, :state, :city, :DOB, :type)";
  $stmt = $conn->prepare($sql);

  $stmt->bindParam(':fullname', $_POST['fullname']);
  $stmt->bindParam(':username', $_POST['username']);
  $stmt->bindParam(':email', $_POST['email']);
  $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
  $stmt->bindParam(':gender', $_POST['gender']);
  $stmt->bindParam(':country', $_POST['country']);
  $stmt->bindParam(':state', $_POST['state']);
  $stmt->bindParam(':city', $_POST['city']);
  $stmt->bindParam(':DOB', $DOB);
  $stmt->bindParam(':type', $accessType);

  $chk = $conn->prepare("SELECT username FROM assignment2 WHERE username =  :name");
  $chk->bindParam(':name', $username);
  $chk->execute();

  if($chk->rowCount() > 0):
    $message = 'Error ! ! User already exists';
    else:
      if( $stmt->execute() ):
        $message = 'Successfully created new user';
          else:
            $message = 'Sorry there must have been an issue creating your account';
          endif; 
      endif;
    endif;
?>
5
  • 1
    How/where is $username set ? Commented May 27, 2017 at 3:05
  • Hi Paul thanks so much for your reply, I try to declare here $all_rows = $chk->fetchAll(PDO::FETCH_ASSOC); foreach($all_rows as $row): $username = $row["username"]; $_GLOBAL['username'] = $username; endforeach; before ` if($chk->rowCount() > 0):` but still no luck Commented May 27, 2017 at 3:38
  • 1
    I do not see that code in what was originally posted ? When I searched for $username (before my first comment) there was only one 'hit' on this page. There is no foreach in the above code. Add an else to your rowCount check, and print 'no rows found' (or whatever) to see what you get. You could also echo $username to see what is output. Commented May 27, 2017 at 3:41
  • A haaa..... I fixed it just simply add this simple code $username = $_POST['username']; what a bumper -.-" thanks so much for your mention about declaration really help me watch out next time ^^" really appreciated Commented May 27, 2017 at 3:52
  • For the record, calling !empty($_POST['day']) && !empty($_POST['month']) && !empty($_POST['year']) AFTER you declare $DOB = date("Y-m-d", strtotime( $_POST['year'].'-'. $_POST['month'].'-'. $_POST['day'])); will not protect your script frim generating Notices/Warnings regarding undeclared variables. Commented Feb 13, 2022 at 0:04

1 Answer 1

1

According to @Paul T. I finally found the solution here is the final code, thanks so much again for your help @Paul T.

  $username = $_POST['username'];
  $chk = $conn->prepare("SELECT username FROM assignment2 WHERE username =  :name");
  $chk->bindParam(':name', $username);
  $chk->execute();


  if($chk->rowCount() > 0):
    $message = 'Error ! ! User already exists';
    else:
      if( $stmt->execute() ):
        $message = 'Successfully created new user';
          else:
            $message = 'Sorry there must have been an issue creating your account';
          endif; 
      endif;
    endif;
Sign up to request clarification or add additional context in comments.

1 Comment

You could also have simply used the $_POST['username'] reference at the bind to avoid an extra variable need. You discovered what I was getting at though, that the $username was not set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.