Hi!
Say I have a database called Poll and have these two table structures. poll table has fields pid (auto_inc) and title. choices table has cid (auto_inc), pid, and text.
How do i add a value to the poll table and at the same time get the pid of the most recent entry in the poll table to be used as the pid in the choices table? So far I have this:
<?php
$sql = "INSERT INTO poll (pid, title) VALUES (NULL, 'sometitle')";
if (mysql_query($sql)) {
// get the latest pid
$pid = mysql_result(mysql_query("SELECT pid FROM poll ORDER BY pid DESC LIMIT 1"), 0);
// use the latest pid retrieved to insert to the "choices" table
$sql = "INSERT INTO choices (cid, pid, text) VALUES (NULL, " . $pid . ", 'sometext')";
mysql_query($sql);
}
?>
I just have a feeling that there's a better solution to this.
Thanks!