Hey guys, I just built php mysql refer system from scratch. I decided to share the source code with you here.

Before I'll leave everything for you, I'll explain everything one after the other.

Before I start explaining the code below. I did share sign up source code here.



php mysql delete statement


Computer shortcut keys



I assume you already have users registered in your website. They even have access to login their accounts.

As a developer, allowing users to login, you need to start seesion and store the user ID in it. Why we do so is because every user has unique ID in the database.






So with the little above explanation, I've already stored the logged in user ID in session. Using that unique ID number, I fetched the row matching that unique ID. Get hold of the username and make a link with it.

Now if a user send that unique link to someone on the internet and the person is redirect to the sign up form, the username is automatically entered in the referral input field.





So if the person fill the form and submit, the referral will count for the person who send out his/her referral link.

refer.php file

<?php
session_start();
//Db connection
$mydb = new mysqli("localhost", "roor", "", "superguideblog");

$user_id = $_SESSION['user_id'];
$slt =("
SELECT * FROM users_table WHERE                      user_id='{$user_id}'");
$result = mysqli_query($mydb,$slt);

        echo "Below is your unique referral link.            Share it but don't abuse other platforms              with it.";

if($result)
{
while($row = mysqli_fetch_assoc($result))
{
$id = $row['user_id'];

$username = $row['username'];

        echo "<a href='sign-up.php?                                    username={$username}'></a>";
}
}
 <input type="" name=""                                         value="https://www.superguideblog.com/sign-   up.php?user=<?php echo $username;?>"   style="width:90%;height:20px;text-     align:center;font-weight:bold;"/>
?>

Now below is the sign up form, where the referral link will redirect the clickers.

Note: The referral username is automatically typed in but the input field is hidden. This refer system is not secure. You can make it secure by yourself.

sign-up.php file

<?php
session_start();
//Db connection
$mydb = new mysqli("localhost", "roor", "", "superguideblog");

 if(isset($_SESSION['user_id']))
 {
 header("Location: profile.php");
 }

  //Store registration failed and
  //already used username and email variables
$errorreporter = "";
$usernameused = "";
$usernameused1 = "";

//Store empty error variables
$usernameErr = "";
$invalidEmail = "";
$passwordErr = "";
$phoneErr = "";

//Store empty error limit variables
 $dobLmt = "";

//Store form data variables
 $username = "";
 $user_first_name = "";
 $user_surn_name = "";
 $user_email = "";
 $user_password = "";
 $user_phone = "";
 $refer_username = "";
 $user_agree = "";
 $user_date = date("Y:m:d");

 if(isset($_POST['submit']))
 {
 if(isset($_POST["username"]))
 {
 $username = ($_POST["username"]);
 }

 if(isset($_POST["user_first_name"]))
 {
 $user_first_name = ($_POST["user_first_name"]);
 }

 if(isset($_POST["user_surn_name"]))
 {
 $user_surn_name = ($_POST["user_surn_name"]);
 }

 if(empty($_POST["user_email"]))
 {
 $emailEm = "Email is required";
 }
 else
 {
 $user_email = ($_POST["user_email"]);
 }


 if(!filter_var($user_email,FILTER_VALIDATE_EMAIL))
 {
 $invalidEmail = "Email address is invalid";
 }


if(empty($_POST["user_password"]))
{
$passwordEm = "Password is required";
}
else
{
$user_password = ($_POST["user_password"]);
}


if(!preg_match("/^[a-z0-9]*$/",$user_password))
{
$passwordErr = "Password must contain alphabet and intiger";
}



if($user_password < 8)
{
$passwordLmt = "Password must be 8 and above";
}


if(empty($_POST["user_phone"]))
{
$phoneEm = "Phone number is required";
}
else
{
$user_phone = ($_POST["user_phone"]);
}



if(!preg_match("/^[0-9]*/",$user_phone))
{
$phoneErr = "Phone number must be digits only!";
}



if($user_phone < 11)
{
$phoneLmt = "Phone number must be 11 digit";
}


$user_agree = ($_POST["user_agree"]);

$refer_username = ($_POST["refer_username"]);

$user_status = ($_POST["user_status"]);

$mysl = "
SELECT * FROM users_table WHERE  username='{$username}'";
$mysltq = mysqli_query($mydb,$mysl);
$found = mysqli_num_rows($mysltq);

if($found)
{
$usernameused = "<br /> Sorry, <b style='color:black;'>{$username}</b> is already taken!";
}
else
{

$mysls = "
SELECT * FROM users_table WHERE  user_email='{$user_email}'";
$mysltqs = mysqli_query($mydb,$mysls);
$founds = mysqli_num_rows($mysltqs);

if($founds)
{
$usernameused1 = "<br /> Sorry, <b style='color:black;'>{$user_email}</b> is already used by another member!";
}
else
{
$username = $mydb->real_escape_string($username);

$user_first_name = $mydb->real_escape_string($user_first_name);

$user_surn_name = $mydb->real_escape_string($user_surn_name);

$refer_username = $mydb->real_escape_string($refer_username);

$user_email = $mydb->real_escape_string($user_email);

$user_password = $mydb->real_escape_string($user_password);

$user_phone = $mydb->real_escape_string($user_phone);

$user_status = $mydb->real_escape_string($user_status);

$user_date = $mydb->real_escape_string($user_date);

$user_agree = $mydb->real_escape_string($user_agree);

$su = $mydb->prepare("INSERT INTO users_table (username,user_first_name,user_surn_name,refer_username,user_email,user_password,user_phone,user_status,user_date,user_agree) VALUES(?,?,?,?,?,?,?,?,?,?)");

$su->bind_param('ssssssssss', $username, $user_first_name, $user_surn_name, $refer_username, $user_email, $user_password, $user_phone, $user_status, $user_date, $user_agree);
$su->execute();

if($su)
{
header("Location: sign-in.php");
}
else
{
$errorreporter = "Sorry, your registration was unsuccessful. Please check properly and retry again. Thank you!";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>sign up form</title>
</head>
<body>
<?php
echo "<p style='font-weight:bold;color:red;'>";
echo $usernameused;
echo "</p>";
echo "<p style='font-weight:bold;color:red;'>";
echo $usernameused1;
echo "</p>";
echo "<p>";
echo $errorreporter;
echo "</p>";
?>

<fieldset>
<legend style="font-weight:bold;font-size:25px;">Registration form</legend>
<h3>Register for free</h3>
<form action="" method="POST" class="signup-form-class"><br />

<label>Username:</label><br />
<input type="text" name="username" required="required" placeholder="e.g boss" class="input-field-ele" /><br /><br />

<label>First Name:</label><br />
<input type="text" name="tech" required="required" placeholder="e.g godgift" class="input-field-ele" /><br /><br />

<label>Surname:</label><br />
<input type="text" name="teacher" required="required" placeholder="e.g theophilus" class="input-field-ele"/>

<input type="hidden" name="refer_username" value="<?php if(isset($_GET["user"])){$user = $_GET["user"];if(!$user == ""){$find = "SELECT * FROM users_table WHERE username='{$user}' AND user_status='active' AND ref_pay='paid'"; $result = $mydb->query($find);if($result){echo $user;}}}?>" class="input-field-ele" /><br /><br />

<input type="hidden" name="user_status" value="active" />

<label>Email Address:</label>
<?php echo "<small>"; echo $invalidEmail; echo "</small><br />"; ?>

<input type="email" name="user_email" required="required" placeholder="e.g example@gmail.com" class="input-field-ele" /><br />

<?php echo "<small>"; echo $passwordErr; echo "</small><br />"; ?>

<label>Password:</label><br />
<input type="password" name="user_password" required="required" minlength="8" placeholder="e.g 9jab2018" class="input-field-ele" /><br />

<?php echo "<small>"; echo $phoneErr; echo "</small><br />"; ?>

<label>Cell Phone:</label><br />
<input type="text" name="user_phone" required="required" minlength="11" maxlength="11" placeholder="e.g 09050330311" class="input-field-ele" /><br />

<input type="checkbox" name="user_agree" value="agreed" checked="checked" required="required" />Terms and conditions accepted<br />

<input type="submit" name="submit" value="submit" class="submit-button" /><br />
</form>
</fieldset>
</body>
</html>





Ask any question base on this refer system.