Link to home
Start Free TrialLog in
Avatar of grantunwin
grantunwin

asked on

mysql_insert_id()...............problem

I need to be a able to add a record to one of the tables. And then create another record in another table using the primary key from the previous table.

$sql="INSERT INTO `signup` (`actname` , `email` , `password` , `firstname` , `lastname` , `bdate` , `gender` , `aboutme` , `homepage` , `region` , `occupation` , `ethnic` , `account_status` , `hair` , `eye` , `height` , `agent` , `cat_models` , `cat_actors` , `cat_extras` , `cat_tv` , `cat_music` , `cat_dance` , `cat_audience` , `cat_other` , `phone` , `mobile` , `interest` , `hear` , `sendlist` , `newsletter` , `personal` , `experience` , `exp_tv` , `exp_music` , `exp_dance` , `exp_other` , `nickname` , `area` ) VALUES ('". $_REQUEST[actname] ."', '". $_REQUEST[email] ."', '". $_REQUEST[password] ."', '". $_REQUEST[firstname] ."', '". $_REQUEST[lastname] ."', '". $bdateholder ."', '". $_REQUEST[gender] ."', '". $_REQUEST[aboutme] ."', '". $_REQUEST[homepage] ."', '". $_REQUEST[region] ."', '". $_REQUEST[occupation] ."', '". $_REQUEST[ethnic] ."', 'Inactive', '". $_REQUEST[hair] ."', '". $_REQUEST[eye] ."', '". $_REQUEST[height] ."', '". $_REQUEST[agent] ."', '". $_REQUEST[cat_models] ."', '". $_REQUEST[cat_actors] ."', '". $_REQUEST[cat_extras] ."', '". $_REQUEST[cat_tv] ."', '". $_REQUEST[cat_music] ."', '". $_REQUEST[cat_dance] ."', '". $_REQUEST[cat_audience] ."', '". $_REQUEST[cat_other] ."', '". $_REQUEST[phone] ."', '". $_REQUEST[mobile] ."', '". $_REQUEST[interest] ."', '". $_REQUEST[hear] ."', '". $_REQUEST[sendlist] ."', '". $_REQUEST[newsletter] ."', '". $_REQUEST[exp_models] ."', '". $_REQUEST[exp_actors] ."', '". $_REQUEST[exp_tv] ."', '". $_REQUEST[exp_music] ."', '". $_REQUEST[exp_dance] ."', '". $_REQUEST[exp_other] ."', '". $_REQUEST[nickname] ."', '". $_REQUEST[area] ."')";
$conn->execute($sql);

//////////////////////SET newUID AS NEW UID/////////////////////////////
$newUID = mysql_insert_id();


$newUID is then set to a massive number like 14100041 but when I use PHPmyadmin to look at the table the primary key is in actual fact 234. Im very puzzled by this if anyone could help it would be much appreciated.

Regards
Avatar of gamebits
gamebits
Flag of Canada image

Try

LAST_INSERT_ID()
Avatar of grantunwin
grantunwin

ASKER

Fatal error: Call to undefined function: last_insert_id() in /var/www/vhosts/myauditionmovie.com/httpdocs/payscript.php on line 19
>> $conn->execute($sql);

You appear to be accessing your MySQL connection via an object. Are you sure that the connection is accessible when you run mysql_insert_id() directly? Perhaps you need to create a new method in the class to retrieve the insert id.
Slightly less efficient but does the trick.

$sql="SELECT MAX(UID) AS total FROM `signup`";

$conn->execute($sql);

$newUID = mysql_result($total, 0, 0);
 
Depending on the fragmentaion of your data, that may or may not work. You will still want a method to identify the last insert ID.

If you have access to the object declaration, can you add a method that will simply return the last insert ID? Something like:

class myConnection()
{
   // Other code will be here

   function getLastInsertId()
   {
      return mysql_insert_id($this->connection;)
   }
}

Then you will access the insert id like this:

$newUID = $conn->getLastInsertId();
$conn = &ADONewConnection($DBTYPE);
$conn->PConnect($DBHOST, $DBUSER, $DBPASSWORD, $DBNAME);
Sorry, I guess I mean the place where ADONewConnection is defined. There you will find the methods such as PConnect() and execute(). You will want to modify that object definition to return the insert id.
ASKER CERTIFIED SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial