Reputation: 25
I'm having trouble with a PHP project I was given. More particularly the MYSQL database is troubling me.. Here is the idea of the whole thing:
I have made a database already with 3 tables - T1, T2, Log. T1 stores name and code of a employer; T1 stores name and code of a worker; Log stores ID (auto incremented), employer name, worker name, start date and end date (of work).
I made a form with two fields - Code for Employer and Code for Worker. My PHP code checks the codes input in the fields (checks if they are empty and then checks if they match the information in T1 and T2). Then with a Submit button all data is sent and stored into Log Table. But I had to add two radio buttons in there: one for the Worker start time and one for his end time. When pressed the Start radio button, the sql request does the following - inserts name of employer, name of worker and start the start date. This part here works flawlessly. My End Date column is set to place current date upon update.
If the End radio button is pressed, I want the following to happen: find the LAST row with the selected in form names for emp and worker (they input codes in the form which are checked and the actual names are stored as variables). So is it possible to make something like:
SELECT ID FROM Log ORDER BY ID DESC LIMIT 1
WHERE Worker_Name='$nameW' and Employer_Name='$nameE'
I say something like it, because it gives me an error in the whole WHERE part and does not accept my query..
Any help would be appreciated. Thanks in advance!
Upvotes: 0
Views: 334
Reputation: 2769
Your query is not correct you have to put the order by and the limit part at the end (i.e. after the where condition). The correct query for your situation will be:
SELECT ID FROM Log
WHERE Worker_Name='$nameW' and Employer_Name='$nameE' ORDER BY ID DESC LIMIT 1
Upvotes: 1
Reputation: 581
Your query is wrong use it as below :
SELECT `ID` FROM `Log`
WHERE `Worker_Name`='{$nameW}' and `Employer_Name`='{$nameE}'
ORDER BY `ID` DESC LIMIT 1
Upvotes: 1