• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

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
0
grantunwin
Asked:
grantunwin
  • 4
  • 3
1 Solution
 
gamebitsCommented:
Try

LAST_INSERT_ID()
0
 
grantunwinAuthor Commented:
Fatal error: Call to undefined function: last_insert_id() in /var/www/vhosts/myauditionmovie.com/httpdocs/payscript.php on line 19
0
 
glcumminsCommented:
>> $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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
grantunwinAuthor Commented:
Slightly less efficient but does the trick.

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

$conn->execute($sql);

$newUID = mysql_result($total, 0, 0);
 
0
 
glcumminsCommented:
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();
0
 
grantunwinAuthor Commented:
$conn = &ADONewConnection($DBTYPE);
$conn->PConnect($DBHOST, $DBUSER, $DBPASSWORD, $DBNAME);
0
 
glcumminsCommented:
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.
0
 
glcumminsCommented:
Aha! I think I have found it. I assumed that you were using an object that you had designed an implemented yourself, but I searched for ADONewConnection and found the definition online ().

The function you want to use is Insert_ID(http://phplens.com/lens/adodb/docs-adodb.htm#connect ). To retrieve the insert ID, use:

$newUID = $conn->Insert_ID( );

See if that returns a reasonable value for you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now