By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

Getting an auto increment value after creating a new record

AaronL
P: 99
Hello,

First I would like to say thank you all for your help in the past. I am stumped again.

I am creating an e-commerce system and I want to be able to upload images to the server and assign them to products. For the product ID, I have an auto increment value for the primary key to avoid duplicate item numbers. Since I do not want to pass all of my record values to my image upload program (when I write it), I assume to just submit the product to the database first and then bring the user to an image upload screen. Now these images are going to be associated with the product ID, but when I submit the new product, I don't know what that product ID is going to be. Is there a way to obtain this value for the new database record and then pass it to an image upload program.

Also, what would be the best way to write an image upload program, I want the maximum to be 5 images per product. Attached is the code I have written so far before product submission. I hope someone can shed some light on this for me.

Expand|Select|Wrap|Line Numbers
  1. $catid = $_GET[catid];
  2. $itemno = $_GET[itemno];
  3.  
  4. echo "<form action='submititem.php' method='post' enctype='multipart/formdata'>
  5. <table align='center' border='1'>";
  6. if($itemno > 0)
  7. {
  8.   //Get product information
  9.   echo "<tr><td align='center' colspan='3' bgcolor='#C0C0C0'><b>Edit Item</b></td></tr>";
  10.   $buildsql = "SELECT * FROM products WHERE itemno=" . $itemno;
  11.   $result = mysql_query($buildsql);
  12.   $rowset = mysql_fetch_array($result);
  13.   //Get all variables from the record
  14.   $itemno = $rowset['itemno'];
  15.   $title = $rowset['title'];
  16.   if($catid == '')
  17.   {
  18.     $catid = $rowset['catid'];
  19.   }
  20.   $list = $rowset['list'];
  21.   $price = $rowset['price'];
  22.   $taxable = $rowset['taxable'];
  23.   $shipping = $rowset['shipping'];
  24.   $intshipping = $rowset['intshipping'];
  25.   $calculator = $rowset['calculator'];
  26.   $description = $rowset['description'];
  27.   echo "<tr><td><b>Item Number: </b>" . $itemno . "</td>";
  28. else
  29. {
  30.   echo "<tr><td align='center' colspan='3' bgcolor='#C0C0C0'><b>New Item</b></td></tr>
  31. <tr><td align='center' bgcolor='#FF0000'><b>New Item</b></td>";
  32. }
  33. //Get item selected catagory
  34. $catsql = "SELECT * FROM catagories WHERE catid=" . $catid;
  35. $catres = mysql_query($catsql);
  36. $catrow = mysql_fetch_array($catres);
  37. $parent = $catrow['parent'];
  38. $catdesc = $catrow['description'];
  39. while($parent <> 0)
  40. {
  41.   $catsql = "SELECT * FROM catagories WHERE catid=" . $parent;
  42.   $catres = mysql_query($catsql);
  43.   $catrow = mysql_fetch_array($catres);
  44.   $parent = $catrow['parent'];
  45.   $catdesc = $catrow['description'] . " / " . $catdesc;
  46. }
  47. echo "<td colspan='2'><b>Catagory: " . $catdesc . "</b><input type='hidden' value=" . $catid . " name='catid' />";
  48. if($itemno > 0)
  49. {
  50.   echo "<a href=catagory.php?itemno=" . $itemno . ">[edit]</a>";
  51. }
  52. echo "</td></tr>";
  53. echo "<tr><td colspan='3' align='center'><b>Title:</b><input type='text' size='50' value='" . $title . "' name='title' />";
  54. echo "</td></tr><tr><td><b>List Price:</b> $<input type='text' size='5' name='list' value='" . $list . "' /></td>";
  55. echo "<td colspan='2'><b>Our Price:</b> $<input type='text' size='5' name='price' value='" . $price . "' />";
  56. echo " <input type='checkbox' name='taxable' checked>Taxable?</td></tr>";
  57. echo "<tr><td><b>Shipping:</b> $<input type='text' name='shipping' size='5' value='" . $shipping . "' /></td>";
  58. echo "<td><b>International Shipping:</b> $<input type='text' name='intshipping' size='5' value='" . $intshipping . "' /></td>";
  59. echo "<td><input type='checkbox' name='shipcalc'>Use Calculator</td></tr>";
  60. echo "<tr><td colspan='3'><b>Description:</b><br><center><textarea rows='10' cols='65' name='description'>" .
  61. $description . "</textarea></center>";
  62. //Item submission routine
  63. If($itemno <> 0)
  64. {
  65.   echo "<a href='imgupload.php?itemno=" . $itemno . "' target='upload'>Upload Product Images</a>";
  66. }
  67. echo "<p align='right'><input type='submit' value='Submit' /></p></td></tr></table></form>";
  68.  
May 14 '09 #1
Share this Question
Share on Google+
1 Reply


prabirchoudhury
100+
P: 162
1. when you add a new product in the database then retrieve the newly inserted product_id frm database.

2. execute first insert query
Expand|Select|Wrap|Line Numbers
  1. mysql_query($your_insert_query)or die("cant execute: $your_insert_query");
  2.  
  3.  
  4. // retrieve just inserted product_id 
  5.  
  6. $product_id = mysql_insert_id();
  7.  
  8.  
  9.  

use this $product_id for associated image

hope that gonna help..
May 14 '09 #2

Post your reply

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