473,396 Members | 2,139 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,396 software developers and data experts.

php: duplicate when inserting and updating 2 tables

Hi, I'm trying to insert and update 2 tables, purchase and stock. The idea is when the user inserts from form the data is inserted into both tables, but when the user inserts an item that already exist in stock database it only updates the quantity so there's no duplicate and inserts it into purchase table (to be recorded as a purchase). The current code i wrote below is still give duplicate on stock table. I don't really understand the logic for such action. Help is much appreciated, thanks!

here's the form code snippet:

Expand|Select|Wrap|Line Numbers
  1. <form name="purchaseform" method="post" onsubmit="return validateForm()" action="submitpurchaseadmin.php">
  2. <table>
  3.     <tr>
  4.         <td>Form number</td>
  5.         <td><input type="text" name="no"></td>
  6.     </tr>
  7.     <tr>
  8.         <td>Item</td>
  9.         <td>
  10.         <select name="item">
  11.         <option value="item 1">Item 1</option>
  12.         <option value="item 2">Item 2</option>
  13.         </select>
  14.         </td>
  15.     </tr>
  16.     <tr>
  17.         <td>Quantity</td>
  18.         <td><input type="text" name="qty"></td>
  19.     </tr>
  20.     <tr>
  21.         <td>Date</td>
  22.         <td><input type="text" name="date" value="<?php echo date("d-m-Y"); ?>"></td>
  23.     </tr>
  24.     <tr>
  25.         <td colspan="2" align="center"><input type="submit" name="submit" value="Save"></td>
  26.     </tr>
  27. </table>
  28. </form>
  29.  
and here's the submit code snippet:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("connect.php");
  3.  
  4. $host="localhost";
  5. $user="root";
  6. $pass="";
  7. $db_name="proyek";
  8. $tbl_name="purchase";
  9.  
  10. mysql_connect("$host", "$user", "$pass")or die("Cannot connect to SQL."); 
  11. mysql_select_db($db_name);
  12.  
  13. $no=$_POST['no'];
  14. $item=$_POST['item'];
  15. $qty=$_POST['qty'];
  16. $date=$_POST['date'];
  17.  
  18. $query=("SELECT * FROM stock WHERE item='$item'");
  19. $result=mysql_query($query);
  20. $row=mysql_num_rows($result);
  21.  
  22. mysql_query("INSERT into return (no, item, qty, date) VALUES ('$no', '$item', '$qty', '$date')");
  23.  
  24. if($row==0)
  25. {
  26.     mysql_query("INSERT into purchase (item, qty, date) VALUES ('$item', '$qty', '$date')");
  27.     mysql_query("INSERT into stock (item, qty) VALUES ('$item', '$qty')");
  28. }
  29. if($type==$row[2])
  30. {
  31.     mysql_query("INSERT into purchase (item, qty, date) VALUES ('$item', '$qty', '$date')");
  32.     mysql_query("UPDATE stock SET qty=qty+'$qty' WHERE item='$item'");
  33. }
  34. else
  35. {
  36.     mysql_query("INSERT into purchase (item, qty, date) VALUES ('$item', '$qty', '$date')");
  37.     mysql_query("INSERT into stock (item, qty) VALUES ('$item', '$qty')");
  38. }
  39. header("location:purchasehistory.php");
  40. ?>
  41.  
Dec 8 '13 #1
1 1223
Dormilich
8,658 Expert Mod 8TB
what you want is INSERT … ON DUPLICATE KEY UPDATE …
Dec 8 '13 #2

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

Similar topics

4
by: Joshua | last post by:
Is there a way to set up Apache to invoke a PHP equest when a file (a image for example) is downloaded? Say, then, that http://www.foo.com/downloads/app1.zip is downloaded, Id like to have...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
0
by: xazos79 | last post by:
Hi All, I currenly have a GridView control i'm using to update values in a database. The directly bound fields update fine. However, there are a couple of places where i use EditItemTemplate...
0
by: athos | last post by:
Hi guys, please help. What I did is : Step1. create a Database named , it's default owner is Step2. create a User in Database named , which maps to the Login on this Database Server, it's a...
3
by: athos | last post by:
Hi guys, please help. What I did is : Step1. create a Database named , it's default owner is Step2. create a User in Database named , which maps to the Login on this Database Server, it's a...
0
by: Nick | last post by:
How do I go about setting a default value for a row when inserting a new record with the DetailsView ? Effectively I need to access the underlying data source and set a column to be a default value...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
1
by: jaishu | last post by:
When i try to insert data into a backend Oracle table (with ODBC) using a form, (i use docmd.runsql "insert....") Access gives me a message like"You are trying to append 1 row to the table are you...
0
by: sree078 | last post by:
Hi I want to delete the duplicate rows from two tables and get the resultant non-duplicate rows from both the tables into another table
1
by: DanielLauJJ | last post by:
When inserting a record into a table, I want SQL Server to generate a number automatically for the Primary Key. (e.g. OrderID is 1, 2, 3 and so on) How to do it? (This behavior is similar to the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.