Forum Moderators: coopster

Message Too Old, No Replies

PHP/MYSQL: After inserting a record with random id how do I return it

         

el_roboto

12:18 pm on Apr 1, 2007 (gmt 0)

10+ Year Member



I'm building a simple ecard script

Once a card has been submitted php generates an id

$db_id = uniqid(rand (),false);

and creates a new record for the e-card in my database

Within the same loop it sends an email to the recipient notifying them of the e-card and it's location - fun/ecard.php?id=

Ofcourse if I write the location as id=$db_id I get a brand new ID instead of the ID for the record that was just created.

So I'm wondering if I can return the value of the ID field for the record that's just been inserted?

Whilest card_id is the primary key it does not auto increment so mysql_insert_id() isn't useful, or so I understand?

Birdman

12:25 pm on Apr 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whilest card_id is the primary key it does not auto increment

Why not? I don't see the point in bending over backwards to do something that's already built-in. Ok, sorry...ramble over.

$db_id should remain the same until you run the unique command again. Is this done in a loop? Maybe you need to move something up in the script.

el_roboto

12:48 pm on Apr 1, 2007 (gmt 0)

10+ Year Member



hello

I don't have it auto incrementing as the id's are generated at random, the reason I'm doing this is to retain some level of privacy for the cards that have been created

If auto incrementing would be easy to browse through id=1 id=2 and so on.


if ($sendcard){$db_id = uniqid(false);
$sql2 = "INSERT INTO ecard (card_id, card_ref, sender_ad, sender_nm, recip_ad, recip_nm, card_msg) VALUES ('$db_id', '$card_ref', '$db_senderad', '$db_sendernm', '$db_recipad', '$db_recipnm', '$db_msg')";
$result2 = mysql_query($sql2);
if (!$result2) {
$message = "Failed to create Ecard.". mysql_error();
} else {
$message = "Your Ecard has been sent! You can see it here http://www.example.net/fun/ecard.php?id=THE_SAME_ID_AS_THE_RECORD_INSERTED"
};

[edited by: jatar_k at 1:51 pm (utc) on April 1, 2007]
[edit reason] please use example.net [/edit]

el_roboto

1:56 pm on Apr 1, 2007 (gmt 0)

10+ Year Member



Never mind I decided to take the easy way out

added a regular auto incrementing id field to the table and then used this to reference the unique id