php - How to automatically subtract or add data from columns in mysql
I am building a system for doctors and patients where the patients submit the type of the medical examination and the quantity of medicine they use. I want to be able to subtract the value of the quantity in the database with PHP but without the user input. The type of the examination and the quantity are on different tables. Is that possible?
What I have done so far :
$records = $handler->query("INSERT INTO exam(id, code, name, medical_exam) VALUES('', '$_POST[code]', '$_POST[name]', '$_POST[medical_exam]')");
And I want to also do this:
$records = $handler->query("UPDATE quantity AND SET quantity = quantity -10");
is that possible within 1 MySQL statement??
Answer
Solution:
Yes. Just separate your SQL Statements with semicolon
;
Do something like
Answer
Solution:
Won't this work?
And by the way is the table name "quantity"?! don't forget that>
Answer
Solution:
I would do it in two steps. First read quantity and store it in a php variable. Then reduce it by 10 and finally update the database with the new quantity value. I think it is safer that way.