I have a signup form, in which the user has to fill in name, email, company , password and repeated password. What I am trying to do: Every company has a limited number of signups, therefore I am trying to run a query, which compares the allowed logins/signups in my company MySQL-table to the number of signed up users in my users MySQL-table. But everytime I try it, I receive the error message, that the maximum number of signups is reached (regardless of the number in the company table...).
company table:
companiesId | companiesName | companiesCode | companiesLogins
-------------------------------------------------------------
1 | Comp1 | Comp1Code | 2
-------------------------------------------------------------
2 | Comp2 | Comp2Code | 3
users table:
usersId | usersName | usersCode | usersCompany | usersPwd
-------------------------------------------------------------
1 | Name1 | Code1 | Comp2 | Pwd1
-------------------------------------------------------------
2 | Name2 | Code2 | Comp1 | Pwd2
-------------------------------------------------------------
3 | Name3 | Code3 | Comp1 | Pwd3
Here for example should no new signup for Comp1 be possible and 2 more for Comp2.
I am working with my PHP-Code "splitted" in different files.
File singup.php (contains specific signup stuff):
<?php if (isset($_POST["submit"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$company = $_POST["company"];
$pwd = $_POST["pwd"];
$pwdRepeat = $_POST["pwdrepeat"];
require_once 'dbh.php';
require_once 'functions.php';
if (maxSignups($conn, $company) !== false) {
header("location: ../page.php?error=maxsignups");
exit();
} ... some more if statements...
createUser($conn, $name, $email, $company, $pwd);} else {
header("location: ../login.php");
exit();}
File functions.php (contains all general functions):
<?php
function maxSignups($conn, $company) {
$sqlLogins = "SELECT companiesLogins AS number FROM companies WHERE companiesName = ?";
$stmtLogins = mysqli_prepare($conn, $sqlLogins);
mysqli_stmt_bind_param($stmtLogins, 's',$company);
mysqli_stmt_execute($stmtLogins);
$Logins = mysqli_stmt_num_rows ($stmtLogins );
//echo json_encode($Logins);
$sqlUsers = "SELECT COUNT(*) AS number FROM users WHERE usersCompany = ?";
$stmtUsers = mysqli_prepare($conn, $sqlUsers);
mysqli_stmt_bind_param($stmtUsers, 's',$company);
mysqli_stmt_execute($stmtUsers);
$Users = mysqli_stmt_num_rows ($stmtUsers );
//echo json_encode($Users);
if($Logins > $Users){
$result = false;
}
else {
$result = true;
}
return $result;
echo json_encode($result);
I am pretty new to PHP and coding in general and because of this I was not really able to adjust some of the possible solutions, which I found here, to my problem. But from my understanding, this approach should be not to far apart (at least logically). Thanks in advance!
If I missed something out to post here feel free to tell me and I will edit the question.
Edit 2020/12/22: I updated my PHP. But I am facing different Problems now...
- Only the Login query (functions.php part 1) runs and for the Users query (functions.php part 2) there occurs following error message:
Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs...\functions.php on line 82
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs...\functions.php on line 83
Warning: mysqli_stmt_num_rows() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs...\functions.php on line 84
is there as problem with running nearly the same script back to back? even though I use different variables?
- The first echo
echo json_encode($Logins);
returns0
but it should return 1 because the name in the input form is the same like in the database table... is there still a problem with my prepared statement even though it is running (at least the first Logins part)?