Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 107,702 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 994 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Need help with retrieving and inserting records with php

 
Reply to this topicStart new topic

Need help with retrieving and inserting records with php

bstonehill
post 27 Jun, 2008 - 06:20 AM
Post #1


New D.I.C Head

Group Icon
Joined: 21 Jan, 2008
Posts: 27



Thanked 1 times
My Contributions


I am creating a page which looks up information about a selected product from a table which contains item templates (pricebook) and then inserts X copies of that item into the items table then creates a relationship between each of those items and the selected purchase order record. The relationships are stored in a seperate table. The problem is that since the id's for each item are generated when they are added to the table and none of the other fields are unique I have no way to then accurately retrieve the item id so that I can add it to the relationship table.

Can anyone think of a way to accomplish this or perhaps a different way of going about it?

Thanks

CODE

    include("connect.php");  //Open database connection
    
    if(isset($_POST['submit'])){ //Check for form submission

      //Lookup item info in pricebook
      $sql = "SELECT * FROM inv_pricebook WHERE name = ".$_POST['item'];
      $result = mysql_query($sql) or
        die("Pricebook Query Failed");
      $row = mysql_fetch_array($result, MYSQL_ASSOC);
      
      //Lookup purchase order ID
      $po_id_sql = "SELECT id FROM inv_purchaseorders WHERE name = '".$_POST['po_number']."'";
      $po_id_result = mysql_query($po_id_sql) or
        die("There was a problem retrieving the purchase order id");
      $po_id_row = mysql_fetch_row($po_id_result);
      
      //Insert items into database
      for($i = 0, $i < $_POST['qty'], $i++){
        $insert_sql = "INSERT INTO inv_items (name, description, part_number, retail_price, item_cost, warranty_period)
                 VALUES ('".$row['name']."', '".$row['description']."', '".$row['part_number']."', '".$row['retail_price']."',
                    '".$row['item_cost']."', '".$row['warranty_period']."')";
        mysql_query($insert_sql) or
          die("There was a problem adding items");
        
        //Insert Item ID into relationship table  
        //?????

         //Insert Purchase Order ID into relationship table
        $sql4 = "INSERT INTO inv_purchasrs_inv_items_c (inv_purchaseorders_ida, inv_purchaseorders_idb)
                 VALUES ('".$po_id_row[0]."', '".$')
      }
    }
User is offlineProfile CardPM

Go to the top of the page


snoj
post 27 Jun, 2008 - 06:53 AM
Post #2


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 4 times

Dream Kudos: 700
My Contributions


You could manually make a "key", like take an MD5 hash of the time and row data (name, description, etc.) and use that for identification when needing that item.

So in your for loop.
CODE

for($i=0;$i<$length;$i++) {
    $hash = md5(time().implode('',$row));
    $sql = "INSERT INTO aTable (keyHash, and, other, fields) VALUES ( '{$hash}', '{$and}', '{$other}', '{$fields}')";
    
    $sql = "SELECT * FROM aTable WHERE keyHash = '{$hash}'";
}
User is offlineProfile CardPM

Go to the top of the page

AdaHacker
post 27 Jun, 2008 - 08:59 AM
Post #3


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 68



Thanked 7 times
My Contributions


Why couldn't you just use mysql_insert_id()?
User is offlineProfile CardPM

Go to the top of the page

snoj
post 27 Jun, 2008 - 09:21 AM
Post #4


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 4 times

Dream Kudos: 700
My Contributions


He could, but it doesn't inherently guarantee that that is the ID he's going to be using since in the time it takes to get the ID and use it, someone else could come along and put something new into the database. Granted the time window for that happening is very very small, but hey, why risk it?

Also, in the above example, the keyHash field should be set to unique so duplicates aren't created.

This post has been edited by snoj: 27 Jun, 2008 - 09:23 AM
User is offlineProfile CardPM

Go to the top of the page

bstonehill
post 27 Jun, 2008 - 12:01 PM
Post #5


New D.I.C Head

Group Icon
Joined: 21 Jan, 2008
Posts: 27



Thanked 1 times
My Contributions


QUOTE(snoj @ 27 Jun, 2008 - 12:21 PM) *

He could, but it doesn't inherently guarantee that that is the ID he's going to be using since in the time it takes to get the ID and use it, someone else could come along and put something new into the database. Granted the time window for that happening is very very small, but hey, why risk it?

Also, in the above example, the keyHash field should be set to unique so duplicates aren't created.


AdaHacker: Thanks, I had no idea that was an option. Turns out however that the keys are not auto-generated by the table like I thought they were, they are generated by the program i'm interfacing with (SugarCRM).

Snoj: Thank you, great idea. I've never seen md5() before either, handy thing. Since I'm duplicating the same item with all the same fields the keys kept coming out the same using time(), so I used microtime() instead and then added the current iteration of $i after $row to ensure they were all unique.

Thanks!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/30/08 01:47AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month