Skip to content
Advertisement

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??

Advertisement

Answer

is that possible within 1 mysql statement??

Yes. Just separate your SQL Statements with semicolon ;

Do something like

$records = $handler->query("INSERT INTO `exam`(`id`, `code`, `name`, `medical_exam`) VALUES('', '$_POST[code]', '$_POST[name]', '$_POST[medical_exam]');UPDATE `quantity` AND SET `quantity` = (`quantity` - 10);");
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement