This topic is locked
[SOLVED]

Limit the number of records users can add

3/26/2021 1:09:08 PM
PHPRunner General questions
Tandy author

I have a few detail table that I need where they can only add one record too. I have followed https://xlinesoft.com/phprunner/docs/limit_the_number_of_records_users_can_add.htm
All I get is data base error and the error is pointing to $data = $rs->fetchAssoc(); part. Here is my break down.
User Table Name: account
Fields needed:
id, password, limit

Details Table Name: trip_load
Fields needed:
trip_load_id, account_id, load_info

The id in account table is the users id and the account_id in the trip_load is also the users id. So the $_SESSION["UserID"] works.

Here is the code I placed in my trip_load Table under Add page, Before record Added.

$limit = 1;

$rs = DB::Query("select count(*) as c from trip_load where account_id = " . $_SESSION["UserID"]);
$data = $rs->fetchAssoc();
$count = $data["c"];

if ($count>= $limit)
{
echo "Limit reached: $count records added already";
exit();
}

As per the manual.
i tested out: select count(*) as c from trip_load in phpMyAdmin and that code works. it came out with 23. That is how many records there are.

I have the field limit in my account Table because I tried that way too: But with the same database error.

Any advice on where I may have gone wrong would be great..

Thanks
James

Sergey Kornilov admin 3/26/2021

Could you show us the exact error message?

Tandy author 3/26/2021

Ok I sort of figured it out. It shows as a server error but when you read the details of the error it shows me the line: "Limit reached: 1 records added already". Why is it a server error and not a pop-up or something?
Also, I figured out I can not use the $_SESSION["UserID"] because I guess it pulls all records not just the one.
So now I am using the master records trip_id and if it is the same it will have the so called server error Limit 1. If there is no record then it will post the new record with no errors.

Here is my code:

//Limit to only one record
$limit = 1;
$rs = DB::Query("select count(*) as c from trip_load where trip_id = " . $values["trip_id"]);
$data = $rs->fetchAssoc();
$count = $data["c"];

if ($count>= $limit)
{
echo "Limit reached: $count records added already";
exit();
}
//End Limit