-1


I am creating custom e-commerce website in PHP OOP. Almost all (shopping system) things are done. I just wanna add referral system. But i am confused how to do.
When customer sign up then referral code/link should generate for that user and when customer share that link with his friends and his friends registered in our website then that customer who refers and who signed up using that referral code/link, both will get reward (price). Even, i didn't understand how to make structure. If you know about how to create this kind of system. Then at least guide me structure and how to create this.
My users table is here: Click to see screenshot

My insert users function:

public function createAccount($data){
    $first_name = mysqli_real_escape_string($this->db->link, $data['first_name']);
    $last_name = mysqli_real_escape_string($this->db->link, $data['last_name']);
    $phone_number = mysqli_real_escape_string($this->db->link, $data['phone_number']);
    $password = mysqli_real_escape_string($this->db->link, $data['password']);

    if($first_name == "" || $last_name == "" || $phone_number == "" || $password == ""){
        $msg = "Required fields can not be empty. Please fill the fields.";
        return $msg;
    } else if($this->checkPhoneNumber($phone_number) === true){
        $msg = "Phone Number is already in use. Try another phone number!";
        return $msg;
    } else {
        $query = "INSERT INTO users(first_name, last_name, phone_number, password, authority, created_on) VALUES('$first_name', '$last_name', '$phone_number', '$password', 'Customer' , NOW())";
        $result = $this->db->insert($query);
        if($result != false){
            $func = $this->loginDuringCreatingAccount($phone_number, $password);
            return $func;
        }
    }
}

public function checkPhoneNumber($phone_number){
    $query = "SELECT `phone_number` FROM users WHERE phone_number = '$phone_number'";
    $result = $this->db->select($query);
    $count = @$result->num_rows;
    if ($count > 0){
        return true;
    } else {
        return false;
    }
}

public function loginDuringCreatingAccount($phone_number, $password){
    $query = "SELECT * FROM users WHERE phone_number = '$phone_number' AND password = '$password'";
    $resultS = $this->db->select($query);
    if($resultS != false){
        $value = $resultS->fetch_assoc();
        Session::set("userlogin", true);
        Session::set("user_id", $value['user_id']);
        Session::set("first_name", $value['first_name']);
        Session::set("last_name", $value['last_name']);
        Session::set("phone_number", $value['phone_number']);
        Session::set("password", $value['password']);
        header("Location: choose-username.php?step=last");
    }
}

Now, i have to update/add some column names in this users table or for that system i have to create new table. And if i have to create new table then i have to join users and that new table or not?

Can you please guide me to create this system?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Zain Shabir
  • 79
  • 3
  • 11
  • Rough idea: Add a referal token to each user. When another user signs up they can enter a referal token from a different user into a new field in the register form, Or it automatically fills in when the token is found in a query parameter. Then when the registration is complete you can give them both their reward. – Dirk Scholten Sep 04 '19 at 07:39
  • Can you please tell me about database table structure? – Zain Shabir Sep 04 '19 at 07:44
  • I am stuck, about database structure. Can you please tell me about database table structure for this system? – Zain Shabir Sep 04 '19 at 07:47
  • I think in its current from, this is rather too broad of a question for this site - and would suggest that you start by typing “how to create a referral system php mysql” or something similar into Google. – misorude Sep 04 '19 at 07:51
  • I have googled but these answers are totally different. – Zain Shabir Sep 04 '19 at 07:52
  • It surprises me that you made a whole shopping system but adding 1 field to the database is now difficult. Or maybe you make it way harder than it needs to be? Just give each user a referel token in the database. Like the user id, but a different field. Then after a user has registered with that referal token you can find who that token belongs to in the database and if a user is found then give them both their reward. – Dirk Scholten Sep 04 '19 at 07:55
  • It's difficult for me just because i didn't done this before brother. Thanks for your help. I understand – Zain Shabir Sep 04 '19 at 07:56

1 Answers1

1

How to create refer and earn system in php oop

Tables required

  • Users (columns: User details with auto-generated refrel code)
  • referral(columns:ReferedBy, ReferedTo, CouponId)
  • Coupon(columns:CouponId, CouponType, CouponAmount)

Flow:

  1. When new user signup, all the details with auto-generated referral code gets inserted in the Users table.
  2. when another new user signup with referral code, all the details with auto-generated referral code gets inserted in the Users table, And we will fetch the userid on the basis of referral, then the data gets inserted in referral table. Coupon id will be based on Coupontype='Referral',
  3. After than coupon will be send to both the users
Priya Goel
  • 26
  • 1