How to make the user register using unique code
I'm building a website and I'd like to limit how many users can register on it.
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
|
show 2 more comments
I'm building a website and I'd like to limit how many users can register on it.
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
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
|
show 2 more comments
I'm building a website and I'd like to limit how many users can register on it.
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
I'm building a website and I'd like to limit how many users can register on it.
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
php html sql phpmyadmin
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
|
show 2 more comments
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
|
show 2 more comments
1 Answer
1
active
oldest
votes
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
I never looked up into ajax and I was trying to do it only using PHP
– TheNoobUser
Nov 28 '18 at 13:23
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
I never looked up into ajax and I was trying to do it only using PHP
– TheNoobUser
Nov 28 '18 at 13:23
add a comment |
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
I never looked up into ajax and I was trying to do it only using PHP
– TheNoobUser
Nov 28 '18 at 13:23
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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