How to make the user register using unique code












-1















I'm building a website and I'd like to limit how many users can register on it.



hp



As you can see from the photo, I have a db in phpMyAdmin called website with two tables in it. One for the codes that I create and one for store the registered users.
I'd have a normal register html form with a field for 'CODE'. I 'd like to stop the user from registering if the code doesn't exist or doesn't match the codes I have in the table. After they register I'd like the code to be deleted from the table but I can't figgure out how to do it. This is my code in
HTML



<form class="login-form" method="POST" action="#">
<input type="text" name="username" required="required">
<input type="password" name ="password" required="required">
<input type="text" name="code" required="required">
<input type="submit" value="Register">
</form>


PHP



<?php
$host = "192.168.11.32:3306";
$user = "cyka";
$password = "blyat";
$db = "website";
$conn = mysqli_connect($host, $user, $password, $db);

if(isset($_POST['code'])){
$userCode = $_POST['code'];
$userUsername = $_POST['username'];
$userPassword = $_POST['password'];
$sql = "SELECT * FROM codes WHERE codice = '$userCode'";
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result)==1){
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if ($conn->query($sql) === TRUE) {
echo "Thank you for registering!";
} else {
echo "Errore: " . $sql . "<br>" . $conn->error;
}
header('location: main.php');
} else {
echo ("The code is not valid!");
header('location: error.php');
}
}
?>









share|improve this question

























  • Perhaps a foreign key?

    – jarlh
    Nov 28 '18 at 10:17











  • What have you done so far? Can we see your code?

    – kerbholz
    Nov 28 '18 at 10:17











  • Welcome to SO. Have you tried anything? Can you post what you tried? SO is not code writing service, however you probably will get help with the existing code.

    – Eugene Anisiutkin
    Nov 28 '18 at 10:20











  • I edited my question. Sorry if it took long I just realized it was poorly made

    – TheNoobUser
    Nov 28 '18 at 13:22











  • Is this question still open/unsolved?

    – Funk Forty Niner
    Dec 5 '18 at 14:12
















-1















I'm building a website and I'd like to limit how many users can register on it.



hp



As you can see from the photo, I have a db in phpMyAdmin called website with two tables in it. One for the codes that I create and one for store the registered users.
I'd have a normal register html form with a field for 'CODE'. I 'd like to stop the user from registering if the code doesn't exist or doesn't match the codes I have in the table. After they register I'd like the code to be deleted from the table but I can't figgure out how to do it. This is my code in
HTML



<form class="login-form" method="POST" action="#">
<input type="text" name="username" required="required">
<input type="password" name ="password" required="required">
<input type="text" name="code" required="required">
<input type="submit" value="Register">
</form>


PHP



<?php
$host = "192.168.11.32:3306";
$user = "cyka";
$password = "blyat";
$db = "website";
$conn = mysqli_connect($host, $user, $password, $db);

if(isset($_POST['code'])){
$userCode = $_POST['code'];
$userUsername = $_POST['username'];
$userPassword = $_POST['password'];
$sql = "SELECT * FROM codes WHERE codice = '$userCode'";
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result)==1){
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if ($conn->query($sql) === TRUE) {
echo "Thank you for registering!";
} else {
echo "Errore: " . $sql . "<br>" . $conn->error;
}
header('location: main.php');
} else {
echo ("The code is not valid!");
header('location: error.php');
}
}
?>









share|improve this question

























  • Perhaps a foreign key?

    – jarlh
    Nov 28 '18 at 10:17











  • What have you done so far? Can we see your code?

    – kerbholz
    Nov 28 '18 at 10:17











  • Welcome to SO. Have you tried anything? Can you post what you tried? SO is not code writing service, however you probably will get help with the existing code.

    – Eugene Anisiutkin
    Nov 28 '18 at 10:20











  • I edited my question. Sorry if it took long I just realized it was poorly made

    – TheNoobUser
    Nov 28 '18 at 13:22











  • Is this question still open/unsolved?

    – Funk Forty Niner
    Dec 5 '18 at 14:12














-1












-1








-1








I'm building a website and I'd like to limit how many users can register on it.



hp



As you can see from the photo, I have a db in phpMyAdmin called website with two tables in it. One for the codes that I create and one for store the registered users.
I'd have a normal register html form with a field for 'CODE'. I 'd like to stop the user from registering if the code doesn't exist or doesn't match the codes I have in the table. After they register I'd like the code to be deleted from the table but I can't figgure out how to do it. This is my code in
HTML



<form class="login-form" method="POST" action="#">
<input type="text" name="username" required="required">
<input type="password" name ="password" required="required">
<input type="text" name="code" required="required">
<input type="submit" value="Register">
</form>


PHP



<?php
$host = "192.168.11.32:3306";
$user = "cyka";
$password = "blyat";
$db = "website";
$conn = mysqli_connect($host, $user, $password, $db);

if(isset($_POST['code'])){
$userCode = $_POST['code'];
$userUsername = $_POST['username'];
$userPassword = $_POST['password'];
$sql = "SELECT * FROM codes WHERE codice = '$userCode'";
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result)==1){
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if ($conn->query($sql) === TRUE) {
echo "Thank you for registering!";
} else {
echo "Errore: " . $sql . "<br>" . $conn->error;
}
header('location: main.php');
} else {
echo ("The code is not valid!");
header('location: error.php');
}
}
?>









share|improve this question
















I'm building a website and I'd like to limit how many users can register on it.



hp



As you can see from the photo, I have a db in phpMyAdmin called website with two tables in it. One for the codes that I create and one for store the registered users.
I'd have a normal register html form with a field for 'CODE'. I 'd like to stop the user from registering if the code doesn't exist or doesn't match the codes I have in the table. After they register I'd like the code to be deleted from the table but I can't figgure out how to do it. This is my code in
HTML



<form class="login-form" method="POST" action="#">
<input type="text" name="username" required="required">
<input type="password" name ="password" required="required">
<input type="text" name="code" required="required">
<input type="submit" value="Register">
</form>


PHP



<?php
$host = "192.168.11.32:3306";
$user = "cyka";
$password = "blyat";
$db = "website";
$conn = mysqli_connect($host, $user, $password, $db);

if(isset($_POST['code'])){
$userCode = $_POST['code'];
$userUsername = $_POST['username'];
$userPassword = $_POST['password'];
$sql = "SELECT * FROM codes WHERE codice = '$userCode'";
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result)==1){
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if ($conn->query($sql) === TRUE) {
echo "Thank you for registering!";
} else {
echo "Errore: " . $sql . "<br>" . $conn->error;
}
header('location: main.php');
} else {
echo ("The code is not valid!");
header('location: error.php');
}
}
?>






php html sql phpmyadmin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 14:12







TheNoobUser

















asked Nov 28 '18 at 10:14









TheNoobUserTheNoobUser

769




769













  • Perhaps a foreign key?

    – jarlh
    Nov 28 '18 at 10:17











  • What have you done so far? Can we see your code?

    – kerbholz
    Nov 28 '18 at 10:17











  • Welcome to SO. Have you tried anything? Can you post what you tried? SO is not code writing service, however you probably will get help with the existing code.

    – Eugene Anisiutkin
    Nov 28 '18 at 10:20











  • I edited my question. Sorry if it took long I just realized it was poorly made

    – TheNoobUser
    Nov 28 '18 at 13:22











  • Is this question still open/unsolved?

    – Funk Forty Niner
    Dec 5 '18 at 14:12



















  • Perhaps a foreign key?

    – jarlh
    Nov 28 '18 at 10:17











  • What have you done so far? Can we see your code?

    – kerbholz
    Nov 28 '18 at 10:17











  • Welcome to SO. Have you tried anything? Can you post what you tried? SO is not code writing service, however you probably will get help with the existing code.

    – Eugene Anisiutkin
    Nov 28 '18 at 10:20











  • I edited my question. Sorry if it took long I just realized it was poorly made

    – TheNoobUser
    Nov 28 '18 at 13:22











  • Is this question still open/unsolved?

    – Funk Forty Niner
    Dec 5 '18 at 14:12

















Perhaps a foreign key?

– jarlh
Nov 28 '18 at 10:17





Perhaps a foreign key?

– jarlh
Nov 28 '18 at 10:17













What have you done so far? Can we see your code?

– kerbholz
Nov 28 '18 at 10:17





What have you done so far? Can we see your code?

– kerbholz
Nov 28 '18 at 10:17













Welcome to SO. Have you tried anything? Can you post what you tried? SO is not code writing service, however you probably will get help with the existing code.

– Eugene Anisiutkin
Nov 28 '18 at 10:20





Welcome to SO. Have you tried anything? Can you post what you tried? SO is not code writing service, however you probably will get help with the existing code.

– Eugene Anisiutkin
Nov 28 '18 at 10:20













I edited my question. Sorry if it took long I just realized it was poorly made

– TheNoobUser
Nov 28 '18 at 13:22





I edited my question. Sorry if it took long I just realized it was poorly made

– TheNoobUser
Nov 28 '18 at 13:22













Is this question still open/unsolved?

– Funk Forty Niner
Dec 5 '18 at 14:12





Is this question still open/unsolved?

– Funk Forty Niner
Dec 5 '18 at 14:12












1 Answer
1






active

oldest

votes


















-1














Submit the form via an ajax request upon hitting 'register' on your form.



Example of an ajax request to a back-end file:



$('#register-form').submit( function(e){
e.preventDefault();
$.ajax({
url : '/includes/register.php',
type : "POST",
data : $('#register-form').serialize(),
success : function(data) {
if (data == 'true') {
//code if successful
} else {
// code if not successful
}
}
}
});

});


All the values in the form will be posted to back-end file.



You can access the 'code' value that they have entered like this:



$theirCode = $_POST['code'];



Get an array of all your codes in your database - You can write a simple select all function:



function getCodes() {
global $db;
try {
$stmt = $db->prepare("SELECT * FROM codes");
$stmt->execute();
return $stmt->fetchall();
} catch (Exception $e) {
echo $e->getMessage();
}
}

$myCodes = getcodes();


All that you need to do now is just loop through your array of codes and check if their code matches with a code in your array.



$match = false;
foreach($myCodes as $code){
if($theirCode == $code){
$match = true;
break;
}
}

if($match == true){
echo 'true';
} else {
echo 'false';
}


Your ajax request will be listening for true/false. Handel the outcome as you wish.



I do my explanation makes sense and is of help. Thanks






share|improve this answer
























  • I never looked up into ajax and I was trying to do it only using PHP

    – TheNoobUser
    Nov 28 '18 at 13:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53517013%2fhow-to-make-the-user-register-using-unique-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














Submit the form via an ajax request upon hitting 'register' on your form.



Example of an ajax request to a back-end file:



$('#register-form').submit( function(e){
e.preventDefault();
$.ajax({
url : '/includes/register.php',
type : "POST",
data : $('#register-form').serialize(),
success : function(data) {
if (data == 'true') {
//code if successful
} else {
// code if not successful
}
}
}
});

});


All the values in the form will be posted to back-end file.



You can access the 'code' value that they have entered like this:



$theirCode = $_POST['code'];



Get an array of all your codes in your database - You can write a simple select all function:



function getCodes() {
global $db;
try {
$stmt = $db->prepare("SELECT * FROM codes");
$stmt->execute();
return $stmt->fetchall();
} catch (Exception $e) {
echo $e->getMessage();
}
}

$myCodes = getcodes();


All that you need to do now is just loop through your array of codes and check if their code matches with a code in your array.



$match = false;
foreach($myCodes as $code){
if($theirCode == $code){
$match = true;
break;
}
}

if($match == true){
echo 'true';
} else {
echo 'false';
}


Your ajax request will be listening for true/false. Handel the outcome as you wish.



I do my explanation makes sense and is of help. Thanks






share|improve this answer
























  • I never looked up into ajax and I was trying to do it only using PHP

    – TheNoobUser
    Nov 28 '18 at 13:23
















-1














Submit the form via an ajax request upon hitting 'register' on your form.



Example of an ajax request to a back-end file:



$('#register-form').submit( function(e){
e.preventDefault();
$.ajax({
url : '/includes/register.php',
type : "POST",
data : $('#register-form').serialize(),
success : function(data) {
if (data == 'true') {
//code if successful
} else {
// code if not successful
}
}
}
});

});


All the values in the form will be posted to back-end file.



You can access the 'code' value that they have entered like this:



$theirCode = $_POST['code'];



Get an array of all your codes in your database - You can write a simple select all function:



function getCodes() {
global $db;
try {
$stmt = $db->prepare("SELECT * FROM codes");
$stmt->execute();
return $stmt->fetchall();
} catch (Exception $e) {
echo $e->getMessage();
}
}

$myCodes = getcodes();


All that you need to do now is just loop through your array of codes and check if their code matches with a code in your array.



$match = false;
foreach($myCodes as $code){
if($theirCode == $code){
$match = true;
break;
}
}

if($match == true){
echo 'true';
} else {
echo 'false';
}


Your ajax request will be listening for true/false. Handel the outcome as you wish.



I do my explanation makes sense and is of help. Thanks






share|improve this answer
























  • I never looked up into ajax and I was trying to do it only using PHP

    – TheNoobUser
    Nov 28 '18 at 13:23














-1












-1








-1







Submit the form via an ajax request upon hitting 'register' on your form.



Example of an ajax request to a back-end file:



$('#register-form').submit( function(e){
e.preventDefault();
$.ajax({
url : '/includes/register.php',
type : "POST",
data : $('#register-form').serialize(),
success : function(data) {
if (data == 'true') {
//code if successful
} else {
// code if not successful
}
}
}
});

});


All the values in the form will be posted to back-end file.



You can access the 'code' value that they have entered like this:



$theirCode = $_POST['code'];



Get an array of all your codes in your database - You can write a simple select all function:



function getCodes() {
global $db;
try {
$stmt = $db->prepare("SELECT * FROM codes");
$stmt->execute();
return $stmt->fetchall();
} catch (Exception $e) {
echo $e->getMessage();
}
}

$myCodes = getcodes();


All that you need to do now is just loop through your array of codes and check if their code matches with a code in your array.



$match = false;
foreach($myCodes as $code){
if($theirCode == $code){
$match = true;
break;
}
}

if($match == true){
echo 'true';
} else {
echo 'false';
}


Your ajax request will be listening for true/false. Handel the outcome as you wish.



I do my explanation makes sense and is of help. Thanks






share|improve this answer













Submit the form via an ajax request upon hitting 'register' on your form.



Example of an ajax request to a back-end file:



$('#register-form').submit( function(e){
e.preventDefault();
$.ajax({
url : '/includes/register.php',
type : "POST",
data : $('#register-form').serialize(),
success : function(data) {
if (data == 'true') {
//code if successful
} else {
// code if not successful
}
}
}
});

});


All the values in the form will be posted to back-end file.



You can access the 'code' value that they have entered like this:



$theirCode = $_POST['code'];



Get an array of all your codes in your database - You can write a simple select all function:



function getCodes() {
global $db;
try {
$stmt = $db->prepare("SELECT * FROM codes");
$stmt->execute();
return $stmt->fetchall();
} catch (Exception $e) {
echo $e->getMessage();
}
}

$myCodes = getcodes();


All that you need to do now is just loop through your array of codes and check if their code matches with a code in your array.



$match = false;
foreach($myCodes as $code){
if($theirCode == $code){
$match = true;
break;
}
}

if($match == true){
echo 'true';
} else {
echo 'false';
}


Your ajax request will be listening for true/false. Handel the outcome as you wish.



I do my explanation makes sense and is of help. Thanks







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 10:51









Juan JJuan J

22039




22039













  • I never looked up into ajax and I was trying to do it only using PHP

    – TheNoobUser
    Nov 28 '18 at 13:23



















  • I never looked up into ajax and I was trying to do it only using PHP

    – TheNoobUser
    Nov 28 '18 at 13:23

















I never looked up into ajax and I was trying to do it only using PHP

– TheNoobUser
Nov 28 '18 at 13:23





I never looked up into ajax and I was trying to do it only using PHP

– TheNoobUser
Nov 28 '18 at 13:23




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53517013%2fhow-to-make-the-user-register-using-unique-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks