Jump to content

Recommended Posts

hey developers,

 

i have problem to get last insert id. btw.. i try to put that code on my mvc creation.

 

on register_model page i write like this:

 public function numbering(){
        $sth = $this->db->prepare('SELECT * FROM member_data');
        $result = $this->db->lastInsertId();
        return $result;
    }
 

and for the controllers page, i write the code like this:

 

$c = $this->model->numbering();
        

foreach ($c as $key => $value) {
$data = $result('id');             
}          
        
$abc['member_id'] = $data;

 

 

but i didn't get the results,.. can you help me?

Link to comment
https://forums.phpfreaks.com/topic/288594-php-pdo-last-insert-id/
Share on other sites

Hi,

 

no offense, but this makes absolutely no sense whatsoever.

  • You're asking for the ID of the last inserted row, but there's no INSERT query in your code.
  • You prepare a SELECT query, but you never execute it.
  • If the method worked, it would return a single number, but then you try to loop over this number as if it was some kind of collection.
  • If the loop worked, it would just overwrite the same variable over and over again.
  • What is $result('id') supposed to be?

Pretty much every line is a mystery. Are you sure you've thought this through?

 

Maybe what you actually want is the biggest member ID? That's one line of code:

$last_member_id = $this->db->query('SELECT MAX(id) FROM member_data')->fetchColumn();
Edited by Jacques1

 

Hi,

 

no offense, but this makes absolutely no sense whatsoever.

  • You're asking for the ID of the last inserted row, but there's no INSERT query in your code.
  • You prepare a SELECT query, but you never execute it.
  • If the method worked, it would return a single number, but then you try to loop over this number as if it was some kind of collection.
  • If the loop worked, it would just overwrite the same variable over and over again.
  • What is $result('id') supposed to be?

Pretty much every line is a mystery. Are you sure you've thought this through?

 

Maybe what you actually want is the biggest member ID? That's one line of code:

$last_member_id = $this->db->query('SELECT MAX(id) FROM member_data')->fetchColumn();

ok, i wil try your code. thx very much.

If you're new to PDO, the best place to start is the PHP manual...

 

http://www.php.net/manual/en/book.pdo.php

yes, i have read all of that, but i still can't understand. i need people to teach me. is that why i'm joining here.. i hope you can help me to learn php here.. thx a lot

on model page :

public function create($abc){
        $insert = $this->db->prepare('INSERT INTO member_data (`member_id`,`username`,`name`,
        `password`,`address`,`phone`,`nik_numb`,`gender`,`role`)
        VALUES(:member_id, :username, :name, :password, :address, :phone, :nik_numb, :gender, :role)
        ');
        
        $insert->execute(array(

        $id = $this->db->lastInsertId();
        ':member_id' => $abc['member_id'],
        ':username' => $abc['username'],
        ':name' => $abc['name'],        
        ':password' => $abc['password'],
        ':address' => $abc['address'],
        ':phone' => $abc['phone'],
        ':nik_numb' => $abc['nik_numb'],
        ':gender' => $abc['gender'],
        ':role' => $abc['role']
        ));
      ........

and controllers page :

 $abc['member_id'] = 0 . date('ymdN') . '00' . $id;

but i still don't get last insert id... please correct what is wrong..
      

Shouldn't you be calling lastInsertId() following the execution of a query? And as Jacques1 said, it doesn't seem clear as to why you've placed that line of code within the array expression? 

 

i really didn't know what to do. i'm very confuse right now..

 

What I am referring to is the execution of this line of code $id = $this->db->lastInsertId();

 

It should be ran following the execution of:

 

$insert->execute(array(

        $id = $this->db->lastInsertId();  //REMOVE THIS LINE
        ':member_id' => $abc['member_id'],
        ':username' => $abc['username'],
        ':name' => $abc['name'],        
        ':password' => $abc['password'],
        ':address' => $abc['address'],
        ':phone' => $abc['phone'],
        ':nik_numb' => $abc['nik_numb'],
        ':gender' => $abc['gender'],
        ':role' => $abc['role']
        ));

 

//AND PLACE IT HERE

What I am referring to is the execution of this line of code $id = $this->db->lastInsertId();

 

It should be ran following the execution of:

 

$insert->execute(array(

        $id = $this->db->lastInsertId();  //REMOVE THIS LINE

        ':member_id' => $abc['member_id'],

        ':username' => $abc['username'],

        ':name' => $abc['name'],        

        ':password' => $abc['password'],

        ':address' => $abc['address'],

        ':phone' => $abc['phone'],

        ':nik_numb' => $abc['nik_numb'],

        ':gender' => $abc['gender'],

        ':role' => $abc['role']

        ));

 

//AND PLACE IT HERE

 

i have try that, but still didn't work.. ;'(

So what are we supposed to do now? We can't see your screen from here.

 

If you want us to help you, then you need to post the code and describe the problem. No, “doesn't work” is not a valid problem description. In fact, we knew that already, because otherwise you probably wouldn't be here.

 

So what's the problem? Does the INSERT query succeed at all? What's the return value of lastInsertId()?

 

And why do you even want the last insertion ID? Don't you set it manually with $abc['member_id']?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.