I am creating a job number system that a few users will be using at the same time. I have created a job number on the php page and then it saves the number to the job sheet and uses this to link other tables to the job.
I take the job number from a table called numbers which then should increment the number by 1 each time the job is submitted ready to create the next job.
But the numbers are not working correctly.
As an example I get 1,2,3,4,8, then 43,44,45,then 105
I cant see why they would jump so much
$job_number_query = "SELECT * FROM numbers";
$job_result =($mysqli-> query($job_number_query)) ;
$job_num = mysqli_fetch_assoc($job_result);
$increment_job_number = $job_num[job_number];
$update_job_number_query = "UPDATE numbers SET job_number = $increment_job_number +1 ";
$mysqli-> query($update_job_number_query);
//echo ($customer_id);
Then I simply insert the $increment_job_number into the jobsheet table.
I am using int for the Job_number field in the table numbers
I cant think of a way to test the numbers. I guess a way is to look through the jobsheets and add another number to there but because more than one user might have a job that hasn't been submitted yet would this also cause problems.
Just increase the value without the first SELECT:
UPDATE numbers SET job_number = job_number +1
You have no where clause on your update query, so you're incrementing the job_number field in ALL records in the table.
It was me that was the technical failure in the end. I have got the incremental numbers on the create page but then unfortunately I had also got the incremental number on the edit pages so every time I edited the pages I then added 1 to the number field in the numbers table.
Related
I am making a website with a sign up function to sign up for specific classes and I need to create a persistent counter that shows how many spots are left for each class. Basically I need something that says, "if the sign up form has been submitted, find the specific class time and subtract 1 from that class's spots remaining int counter. I have a php validation form and can easily code this function but I don't know how to make the int-- change persistent. I have experimented with sessions, cookies, and mysql but none of them are really what I need. Surely there's a straight forward way to subtract 1 from an int var when the form is submitted without resetting the var. Any help is appreciated!
I dont understand what you want, but if you want just substruct 1,and at the same time have a limit in subtraction, Just create table or fields "maxcount" "nowcount". During registration just compare "nowcount" and "maxcount", if "nowcount" == "maxcount", then not substract.
1) Post your code.
2) hoping that your tables are related, i.e one table for number of courses, and another for userid and courseid many to many relationship.
You first store the set number of users in the course table that has the allowed register count. Then whenever a student submits a form, run a check to fetch the remaining count. From that, subtract 1, run an update query to change the value in the course table.
"SELECT total_seats_remaining FROM course WHERE courseid=xx"
Store result in new variable and subtract 1.
"UPDATE course SET total_seats_remaining = 'var' WHERE courseid = xx"
How to limit mysql rows to select newest 50 rows and have a next button such that next 50 rows are selected without knowing the exact number of rows?
I mean there may be an increment in number of rows in table. Well I will explain it clearly: I was developing a web app as my project on document management system using php mysql html. Everything is done set but while retrieving the documents I mean there may be thousands of documents.
All the documents whatever in my info table are retrieving at a time in home page which was not looking good. So I would like to add pages on such that only newest 50 documents are placed in first page next 50 are in second and so on.
But how come I know the exact number of rows every time and I cannot change the code every time a new document added so... numrows may not be useful I think...
Help me out please...
What you are looking for is called pagination, and the easiest way to implement a simple pagination is using LIMIT x , y in your SQL queries.
You don't really need the total ammount of rows you have, you just need two numbers:
The ammount of elemments you have already queried, so you know where you have to continue the next query.
The ammount of elements you want to list each query (for example 50, as you suggested).
Let's say you want to query the first 50 elements, you should insert at the end of your query LIMIT 0,50, after that you'll need to store somewhere the fact that you have already queried 50 elements, so the next time you change the limit to LIMIT 50,50 (starting from element number 50 and query the 50 following elements).
The order depends on the fields you are making when the entries are inserted. Normally you can update your table and add the field created TIMESTAMP DEFAULT CURRENT_TIMESTAMP and then just use ORDER BY created, because from now on your entries will store the exact time they were created in order to look for the most recent ones (If you have an AUTO_INCREMENT id you can look for the greater values aswell).
This could be an example of this system using php and MySQL:
$page = 1;
if(!empty($_GET['page'])) {
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
if(false === $page) {
$page = 1;
}
}
// set the number of items to display per page
$items_per_page = 50;
// build query
$offset = ($page - 1) * $items_per_page;
$sql = "SELECT * FROM your_table LIMIT " . $offset . "," . $items_per_page;
I found this post really useful when I first try to make this pagination system, so I recommend you to check it out (is the source of the example aswell).
Hope this helped you and sorry I coudn't provide you a better example since I don't have your code.
Search for pagination using php & mysql. That may become handy with your problem.
To limit a mysql query to fetch 50 rows use LIMIT keyword. You may need to find & store the last row id(50th row) so that you can continue with 51th to 100th rows in the next page.
Post what you have done with your code. Please refer to whathaveyoutried[dot]com
check this example from another post https://stackoverflow.com/a/2616715/6257039, you could make and orber by id, or creation_date desc in your query
I got the following problem: When I try to increment one column of my mysql table the value get out of sync on high traffic loads.
The task is splited into two things incrementing the column and add a new row
to another table. So if I add 100 rows the counter should stay at 100.
On high loads e.g. 100 request/s it comes to problems with the counter, I got
80 on the counter value but 120 rows are added.
//This is the current increment routine build with an Yii ActiveRecord Class
$id = 123;
$dataRow = ActiveRecordModel::model()->findByPK($id);
$dataRow->counter +=1;
$dataRow->save();
//Add the row
$row = new ActiveRecordModelRow();
$row->operatingSystem = 1;
$row->save();
I think the problem is that some request are handled faster then others and maybe override the values. Hope someone can help me with pointing me in the right direction or had a suggestion how to solve this problem.
Best,
Nils
Can't you use an auto increment field in mysql so that the database handles the increment rather than the PHP. It wouldn't matter how long the processing took as the integer would be set when the record was inserted.
For our recent project we need to include a counter based on date.
For example a page view counter for answer.php is set in mysql table called counter.
Daily access to answer.php is limited to 150(page views). the table counter will store each access and when daily allowance 150 is over then it gives a warning that you exceeded your limit and block the display.
But I am not able to figure out how this can be done on daily basis. I mean when the next day starts how the counter can be reset and start from 0.
The curdate function returns the current date, so something similar to this:
$sql = "SELECT count(*) from logintable where 'logindate' = CURDATE()";
I realise your query would probably involve more tables and fields. This is a very simplistic off the top of my head untested reply, I'm not sure it even works. Just thinking out loud here.
The answer from #stefgosselin was the first thing you'd want to do; then if the count was < 150, insert a row. If the count was 150 or more, reject the query and tell them they had reached their threshold.
Is it 150 pages per user??? or total no matter who. If its per user, then it should be as simple as adding a single counter to that user's record and keep increasing each time a countable "request" comes in, and look at that count as needed to prevent results as restricted.
Then, since a user has no way of back-dating his/her requests, at the beginning of each day (even via some trigger), you can just update your table to reset all counts back to zero...
Update UserLoginTable set RequestCount = 0
Done.
You create a table called loginlog (a log of all the logins)
table loginlog
id integer autoincrement primary key
user_id integer
logindate date
logincount integer
unique key userdate(user_id, logindate)
Next you create a before update trigger on your table
DELIMITER $$
CREATE TRIGGER bu_loginlog_each BEFORE UPDATE ON loginlog FOR EACH ROW
BEGIN
/*Force an error by selecting from a non-existing table*/
/*this will prevent the update*/
IF new.logincount > 150 THEN SELECT * FROM create_error_only_150_logins_allowed
END $$
DELIMITER ;
In your php code do the following:
INSERT INTO loginlog (user_id, logindate, logincount)
VALUES ('$user_id',curdate(),1)
ON DUPLICATE KEY UPDATE logincount = logincount + 1
Test if the insert succeeded.
If the insert/update fails your user has logged in > 150 times and you can refuse the user.
This will only keep one record per user per day. You can even purge past days from the loginlog table after x number of days.
You need a table which maps dates to pageviews
I'm using PHP and PHPMyAdmin to create a small profile site.
I'm giving members an ID number, based on which is the biggest number currently in the database, +1
I did 25 tests before I got the PHP script where I wanted it to be.
I then deleted those 25 entries using PHPMyAdmin.
But now, when my PHP code does this:
function getLatestID() {
$query = "SELECT max(member_id) FROM members";
$result = #mysql_query($query) or showError("unable to query database for user information");
if (!($record = mysql_fetch_array($result))) return null;
return $record[0];
}
I get the wrong number.
Test scenario: the database table holds 3 entries, with ID's 1, 2 and 3.
I start a debugging session and put a breakpoint on the return $record[0].
I check its contents and instead of 3, which is the biggest number, it's 28.
As in 25+3=28, the 25 entries that I allready deleted...
Does anybody know what's causing this and how I can fix it?
It's probably because you have auto_increment set and the query is returning the highest id. When you deleted the other records, you probably didn't reset the auto increment count.
If you're using auto_increment in MySQL then deleting records won't decrease the next value.
You can empty a table with TRUNCATE TABLE mytable - this will reset the value.
You can also change value that auto-increment thinks is the next value to allocate:
ALTER TABLE members AUTO_INCREMENT = 3;
Note that if you put in a value that is less than the current max value in the auto-increment column, it'll change the value to that MAX+1. To see what the current next value is set to, do this:
SHOW CREATE TABLE members;
At the end of the table definition, it'll show "AUTO_INCREMENT = 26" or whatever it's current value is.