473,419 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Subtract quantity from another table

1 Bit
I am making a simple Point of Sale system for personal use and I have two table namely: sales and items. Items to be sold are based on the list of items in the items table. Below is the insert code to the sales table.

Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['sales']))
  2.       {
  3.         $id        =    $_SESSION['posid'];
  4.  
  5.             $sql    = "    INSERT
  6.                         INTO sales (uid, itemname, category, price, quantity, amount, paid, changeP, cashier, salesDate)
  7.                         VALUES        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  8.  
  9.             if($stmt = mysqli_prepare($con, $sql))
  10.                 {
  11.                 mysqli_stmt_bind_param($stmt, "isssssssss", $id, $itemname, $category, $price, $quantity, $amount, $paid, $changeP, $cashier, $salesDate);
  12.  
  13.                 $itemname    = mysqli_real_escape_string($con, $_POST['itemname']);
  14.                 $category    = mysqli_real_escape_string($con, $_POST['category']);
  15.                 $price        = mysqli_real_escape_string($con, $_POST['price']);
  16.                 $quantity    = mysqli_real_escape_string($con, $_POST['quantity']);
  17.                 $amount        = mysqli_real_escape_string($con, $_POST['amount']);
  18.                 $paid        = mysqli_real_escape_string($con, $_POST['paid']);
  19.                 $changeP    = mysqli_real_escape_string($con, $_POST['changeP']);
  20.                 $cashier      = mysqli_real_escape_string($con, $_POST['cashier']);
  21.                 $salesDate  = mysqli_real_escape_string($con, $_POST['salesDate']);
  22.  
  23.               if(mysqli_stmt_execute($stmt))
  24.                 {
  25.                     echo "<script type='text/javascript'>alert('Payment was successfully processed.');
  26.                             </script>";
  27.                     echo "<script>window.location.href='pos.php'</script>";
  28.                 } 
  29.                     else
  30.                     {
  31.                         echo "<script type='text/javascript'>alert('Oops! Something went wrong. Please try again.');
  32.                                 </script>";
  33.                         echo "<script>window.location.href='pos.php'</script>";
  34.                     }
  35.  
  36.                 }
  37.  
  38.             mysqli_stmt_close($stmt);    // Close statement
  39.  
  40.     mysqli_close($con);        // Close connection
  41.  }
  42.  
Now, I want as I insert a purchased item in the sales table, the quantity in the items table to reduce by the number of items being purchased.
I really don't know how to go round this, please help.
Feb 24 '21 #1
1 2738
bakertaylor28
45 32bit
After writing to the sales table, You need to store quantity of items to be reduced as a php variable, and then make a separate SQL call to the items table using UPDATE:
Expand|Select|Wrap|Line Numbers
  1. UPDATE sales 
  2. SET columnname = foo;
  3. $quantity = foo;
  4. UPDATE items
  5. SET columnname = $quantity; 
  6.  
Feb 25 '21 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Caroline | last post by:
I need to update a table by copying a column from another table (having the same structure, but on another database), from the record having the same primary key. 1 - What is the correct...
2
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
5
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for...
2
by: Colm O'Hagan | last post by:
Hi there, I having a problem with a database I'm setting up, I would be delighted if someone out there could help. The database I'm setting up is a task register datebase, it will be used to...
6
by: Sven Pran | last post by:
Probably the answer is there just in front of me only awaiting me to discover it, but: 1: I want to build a query that returns all records in one table for which there is no successful "join"...
3
by: Shannan Casteel via AccessMonster.com | last post by:
I have a table called tblParttest. It has fields PartNo, PartNo2, PartNo3, .. .., PartNo25 and Quantity, Quantity2, Quantity3, ...Quantity25. If I enter "1234" into the PartNo field and "4" into...
7
by: rcamarda | last post by:
I wish to build a table based on values from another table. I need to populate a table between two dates from another table. Using the START_DT and END_DT, create records between those dates. I...
5
by: Rex | last post by:
Hi, I want to change a value in one table depending on the value(s) in another table. I am trying to achieve this in a form. to elaborate I have a many-to-many relationship between tables...
1
by: satishkhachane | last post by:
-------------------------------------------------------------------------------- I want to select one or more columns from one typed datatable and add it to another table . how I can do this ? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.