Re: [php] INSERT and immediately UPDATE

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2009-10-28 at 12:21 -0700, Allen McCabe wrote:

> Hey everyone, I have an issue.
> 
> I need my (employee) users to be able to insert shows into the our MySQL
> database and simultaneously upload an image file (and store the path in the
> table).
> 
> I have accomplished this with a product-based system (adding products and
> uploading images of the product), and accomplished what I needed because the
> product name was unique; I used the following statements:
> 
> $prodName = $_POST['prodName'];
> $prodDesc = $_POST['prodDesc'];
> $prodPrice = $_POST['prodPrice'];
> 
> $query2  = "INSERT INTO product (pID, pName) VALUES (NULL, '$prodName');";
> $result2 = mysql_query($query2) or die(mysql_error());
> 
> $query  = "SELECT pID FROM product WHERE pName = '$prodName';";
> $result = mysql_query($query) or die(mysql_error());
> $row = mysql_fetch_array($result) or die (mysql_error());
> 
> $prodID = $row['pID'];
> 
> 
> I had to select the new product to get the product id to use in the new
> unique image name.
> 
> The problem I am facing now, is that with the shows that my users add will
> have multitple show times; this means non-unique titles. In fact, the only
> unique identifier is the show id. How can I insert something (leaving the
> show_id field NULL so that it is auto-assigned the next ID number), and then
> immediately select it?
> 
> PHP doesn't seem to be able to immediately select something it has just
> inserted, perhaps it needs time to process the database update.
> 
> Here is the code I have now (which does not work):
> 
> $query2  = "INSERT INTO afy_show (show_id, show_title, show_day_w,
> show_month, show_day_m, show_year, show_time, show_price, show_description,
> show_comments_1, show_seats_reqd) VALUES (NULL, '{$show_title}',
> '{$show_day_w}', '{$show_month}', '{$show_day_m}', '{$show_year}',
> '{$show_time}', '{$show_price}', '{$show_description}',
> '{$show_comments_1}', '{$show_seats_reqd}');";
>  $result2 = mysql_query($query2) or die(mysql_error());
> 
>  $query3 = "SELECT * FROM afy_show WHERE *show_id = '$id'*;";
>  $result3 = mysql_query($query3) or die('Record cannot be located!' .
> mysql_error());
>  $row3 = mysql_fetch_array($result3);
>  $show_id = $row3['show_id'];
> 
> How do I select the item I just inserted to obtain the ID number??


Have a look at mysql_insert_id() which returns the auto insert id value
of the last row to be inserted by the current script. If you use if
right after the insert query (without any other queries in between) then
you have the id for that row. Whatever you do, don't look for the
MAX(id) value! I've seen people do this before, and then wonder why the
database gets all corrupted when more than one person uses the system at
the same time!

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux