PHP/MySQL Update Query - php

I have a form with several select and text boxes that gets dynamically generated from a database query.
The number of rows (The form lives in a html table) depends on how many records gets returned from the database.
For instance:
Row 1 - MUStatus(dropdown), IPlan(dropdown), ATT(text box)
Row 2 - MUStatus(dropdown), IPlan(dropdown), ATT(text box)
Row 3 - MUStatus(dropdown), IPlan(dropdown), ATT(text box)
...
...
...
Row 10 - MUStatus(dropdown), IPlan(dropdown), ATT(text box)
Not all rows are going to be updated all the time. Maybe a user gets a 10-row form and updates only one.
My processing script needs to loop through all rows to figure out which one has changed in order to update. This causes
the update query to run 10 times even if the user only updates one row.
How can I get it to update only the number of rows that has been changed on the form?
Currently this is my processing script (not sanatized yet. Just in test mode)
for($i = 0; $i < $_POST['totalRecords']; $i++){
if($_POST['muStatus'] != $providerMUStatusArray[$i] ||
$_POST['att1IncentivePlanDropDown'!= $providerATT1IncentivePlanArray[$i] ||
$_POST['att1AttestationNumber'!= $providerATT1NumberArray[$i]){
UPDATE QUERY GOES HERE
}//END IF
}//END FOR
Any help is appreciated

This is how I would do it: I would add a hidden field in each row called "Updated" with a start value of 0 then use JQuery or just JavaScript to set it to 1 every time a value in that row is changed.
Even better, I would use AJAX to update each row as soon as the user is done editing it.

I would create a session and store the values you expect. Then when the form is submitted you just check to see if the submitted value equals the session saved value. If it does not equal it then process.

Related

How to use Counter enabled loop processing of mysql query?

I have a database named creative_db Table name is store_weblinks. Now inside this table there are several columns that holds the entire site's weblinks. My focus is on this column - weblinks_status
Now weblinks_status contains 2 values = waiting and live
So here's what I intend to do - Update any 10 waiting to live status.
I think I need a loop of some kind that would keep a count of how many successful edits are taking place. Once that hits 10 it will stop processing..
So, its like this -- Check if the current item of weblinks_status is waiting. If it is waiting then change it to live and increment the loop counter else proceed to the next waiting item..
Need your help!
Try this STATEMENT:
UPDATE store_weblinks
SET weblinks_status="live"
WHERE weblinks_status="waiting"
LIMIT 10;
This should work to update any 10 entries from waiting to live.

Why Output of PHP script and MySQL database data are not same?

Please refer to the following image. In this image number (1) is what the field position in my database userdb looks like before I run my code. Number (2) is what change I am expecting in the position field of some rows after I run the code. The value of the position field of second row is primarily 2 what I want to be changed to 3. Similarly, The values of the position field of third, fourth and fifth rows are primarily 3, 4 and 5 respectively what I want to be changed to 4, 5 and 6 respectively. So, I run the following code and in my browser I see what I expect. However, In the database I see wrong data is inserted, like number (3) on the image. Notice that my last expected value of the position field is 6 (for fifth row), but I am getting 6 in all rows!!!
Here is the code I run:
$requestedPosition = 2; $oldPosition = 6;
for($i=$requestedPosition+1; $i <= $oldPosition; $i++){
$query = mysql_query("UPDATE userdb SET position = '$i' WHERE position='{$requestedPosition}' ");
echo 'Old Position: '.$requestedPosition.' is now: '.$i.'<br>';
$requestedPosition++;
}
I think something must be wrong in the mysql query. If I comment out the query I get expected result in browser. However, to insert those data into database I need to run the query but when I do it, I get wrong data inserted in database. I am on windows, running PHP 5.3.13, MySQL 5.5.24.
Most Confusingly, in the for loop, if I change $i=$requestedPosition+1; to $i=$requestedPosition; I get 2,3,4,5 inserted (in 2nd,3rd,4th,5th row respectively) rather than 6,6,6,6. However, I want 3,4,5,6 inserted (in 2nd,3rd,4th,5th row respectively), so I had to use $i=$requestedPosition+1; in for loop, and when I do, Code gets Mad!!! So, do I :( Stuck with this (with bad headache) for last two days!
There's a slight flaw in your logic.
The first time that your loop runs, you're updating one row - you're updating the row with position 2 to be position 3. This is fine.
But the second time the loop runs, you're updating all rows where position is 3 to be position 4 - and there's two of them now, the one from the table, and the one you've just updated. The same happens on the third and fourth iteration.
You can probably do this more elegantly, with one call:
$query = mysql_query("UPDATE userdb SET position = position + 1 WHERE position >={$requestedPosition} AND position <= ${oldPosition}' ");
I've not tested that, but it should at the least give you an idea.

PHP POST WITH DATATABLE PAGINATION

Sorry if I am repeating the question, I have a HTML FORM page which displays the employee_id & attendance check box (IN, OUT) using datatable.
I made pagination with initial load value in the screen as 10, the data are loaded from mysql DB and all the data loads perfectly with pagination. I will update attendance status of each employee which checkbox and submit the form.
Once i submit the form, i called the PHP _POST (if(isset($_POST['save']))) , inside this i am trying to get the value of all employee_id & checkbox value but, i can able to get only first 10 rows, remaining in page 2,3,4,5 are not available, is there any option to get them, i tried with jquery for each too but it also alerts on first 10 rows.
Please help me out.
I need to post the data to database from data table regardless of pagination either using PHP post method (or) Jquery.
If you using pagination by LIMIT in database query there is, of course, no way you would get more than 10 rows, so, your <form> submit will not give you more than those 10 data arrays you can save to database.
Can you explain why you need to update all rows in database? Because limiting by pagination guarantees that other rows will not be affected (if query is right).

MYSQL rotate through rows by date

The query selects the oldest row from a records table that's not older than a given date. The given date is the last row queried which I grab from a records_queue table. The goal of the query is to rotate through the rows from old to new, returning 1 row at a time for each user.
SELECT `records`.`record_id`, MIN(records.date_created) as date_created
FROM (`records`)
JOIN `records_queue` ON `records_queue`.`user_id` = `records`.`user_id`
AND record_created > records_queue.record_date
GROUP BY `records_queue`.`user_id`
So on each query I'm selecting the oldest row min(date_created) from records and returning the next oldest row larger > than the given date from records_query. The query keeps returning rows until it reaches the newest record. At that point the same row is returned. If the newest row was reached I want to return the oldest (start again from the bottom - one full rotate). How is that possible using 1 query?
From the code you have posted, one of two things is happening. Either this query is returning a full recordset that your application is then able to traverse through using it's own logic (this could be some variant of javascript if the page isn't reloading or passing parameters to the PHP code that are then used to select which record to display if the page does reload each time), or the application is updating the records_queue.record_date to bring back the next record - though I can't see any limitations of only fetching a single record in the query you posted.
Either way, you will need to modify the application logic, not this query to achieve the outcome you are asking for.
Edit: In the section of code that updates the queue, do a quick check to see if the value in records_queue.record_date is equal to the newest record. If it is run something like update records_queue set record_date = (select min(theDateColumn from records) instead of the current logic which just updates it with the current date being looked at.

Checkbox filter using Mysql and PHP

In my application there is a filter option with checkboxes.Checkboxes indicates top five categories and all other categories under another single checkbox.That means total I have six checkboxes(top five + other).When all checked which will fetch all values from database table and when all unchecked which never fetch any values from table.Please anybody give any suggestions for my unchecked case...What will be the condition there?
In the unchecked case i wouldn't do a mysql query - cause it is not necessary and you can save that time. Depending on your application and your code you need to implement a switch here and insert the result on your page as if no result (or entry) has been found. For further details more application code is needed.

Categories