0

Im trying to get the last id after inserting data into mysql.I already try using mysql_insert_id() but it seem doesn't work.

Below is the PHP code :

 function addPhoneContact($customername ,$address ,$email ,$comment,$remarks){

        $connection = MySQLConnection();
        $conf = new BBSupervisorConf();
        $log = new KLogger($conf->get_BBLogPath().$conf->get_BBDateLogFormat(),  $conf->get_BBLogPriority() );

        $query="INSERT INTO bb_customer 
                (customername,address ,email ,comment,remarks) 
                VALUES
               ('".$customername."','".$address."','".$email."','".$comment."','".$remarks."');";


            $customerid = mysql_insert_id();
        try {
        $log->LogDebug("Query[".$query."]");
        if (!mysql_query($query)){
            die (mysql_error());      
        }else{
    }
      }catch(Exception $e){
          $log->LogError($e->getMessage());
      }

        closeDB($connection);
        return $customerid;
    }

Thanks for your help.

art
  • 313
  • 2
  • 5
  • 19
  • is your $query executing properly?also why there is an extra semicolon in your $query? – R R Dec 25 '13 at 05:42

2 Answers2

4

Put this line

$customerid = mysql_insert_id();

After you have executed query by

mysql_query($query)

If mysql query is not executed it will not know about id.

Strongly consider not to use mysql_* functions.

Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
chanchal118
  • 3,551
  • 2
  • 26
  • 52
  • -1 for supporting the use of deprecated `mysql_` functions and not suggesting `mysqli` or `PDO` – Nikola Dec 25 '13 at 05:45
  • 3
    @Nikola I appreciate your thoughts. You can also edit the answer. What is stopping you to do that? – chanchal118 Dec 25 '13 at 05:47
  • @chanchal118, nothing is stopping me, but I do not see why would I write this information when it is your answer. I am giving it a vote down, because I believe that the question is not answered in the best way (actually worse than neutral) and therefore giving you a comment how you can improve it. If you do so, I can easily change my vote. I believe the vote down is correct as `mysql_` functions should not be used any more and even if `mysql_insert_id()` would do the work, you should suggest the better way [alternative] – Nikola Dec 25 '13 at 05:52
  • 1
    In SO we do not sit for exam. It is a community. We should always improve question or answer in the best way we can. Asking `Why you have not done that?` is not the best approach. – chanchal118 Dec 25 '13 at 05:56
  • @chanchal118 asking that sort of question is *exactly* what comments are for, and edits should not change the intended meaning of an answer. – Andrew Barber Dec 25 '13 at 06:12
1
 function addPhoneContact($customername ,$address ,$email ,$comment,$remarks){

    $connection = MySQLConnection();
    $conf = new BBSupervisorConf();
    $log = new KLogger($conf->get_BBLogPath().$conf->get_BBDateLogFormat(),  $conf->get_BBLogPriority() );

    $query="INSERT INTO bb_customer 
            (customername,address ,email ,comment,remarks) 
            VALUES
           ('".$customername."','".$address."','".$email."','".$comment."','".$remarks."');";


        $customerid = mysql_insert_id(); //comment this out
    try {
    $log->LogDebug("Query[".$query."]");
    if (!mysql_query($query)){
        die (mysql_error());      
    }else{
}
  }catch(Exception $e){
      $log->LogError($e->getMessage());
  }
    $customerid = mysql_insert_id(); //add here
    closeDB($connection);
    return $customerid;
}
Ankur Dhanuka
  • 1,217
  • 2
  • 11
  • 22