Welcome Guest, Not a member yet? Register   Sign In
this id?
#1

[eluser]smdelektronik[/eluser]
$data = array( 'email' => $username . "this->id" );

$this->db->insert('users', $data);

// "this->id" is ID of this row
what is best way to do this?


thank you
#2

[eluser]Tim Brownlaw[/eluser]
I'm not sure what your question is... Are you wanting to perform an Insert ( new Row ) or an update ( modify an existing row)? . It looks like you are wanting to perform an Update?

You have to keep an eye on what you have written,
So let's see what you will get with what you currently have
if $username = '[email protected]';
then
$data['email'] will equal [email protected]>id
"this->id" is simply a string you are concatenating to your username.

I "think" you are meaning...
$data = array('id => $this->id, 'email'=>$username);
So to reference any class property, in your case id, you need to use $this->id.

So the format for your array is... Which is simply an associative array where the index names are not numeric, but strings (names) that you assign...
Code:
$data = array(
'column_name_1'=>$column_name_1_value,
'column_name_2'=>$column_name_2_value,
...
'column_name_n'=>$column_name_n_value
);
Where column_name_x are the names of your Table Columns with their respective values.

Then you can create a New Table Row using...
Code:
$this->db->insert(‘users’, $data);

NEXT: Still doesn't answer your question...
You have stated that... // “this->id” is ID of this row ( which should be $this->id or it's something else completely!)
Where are you getting this from?

Now I'm going to suggest you go and read up on active records in the user guide cause I'm out of time, but hopefully that should get you started.
Have a look at - $this->db->update();
#3

[eluser]smdelektronik[/eluser]
$this->db->trans_start();

$this->db->select_max('user_id');
$query = $this->db->get('users');
$id = $query->row()->user_id+1;

$data = array(
'username' => $username . $id ,
'password' => $password,
);

$this->db->insert('users', $data);
...
$this->db->trans_complete();


my solution is this, but maybe some1 have better solution.
hope you understand now
#4

[eluser]Tim Brownlaw[/eluser]
Well that's a whole lot different to your original post.

If that works for you, that's a way of doing it.

My only concern is if there is a race condition and two folks signing up at the same time get the same id.

I'm guessing you are trying to make the usernames unique so you could put a unique constraint on the username in the DB Table and/or perform a Select to ensure the username is not already taken and take the appropriate action.

It'd be very unlikely that there would be two identical usernames submitted that would end up having the same id... But not impossible.
#5

[eluser]smdelektronik[/eluser]
that i use to create multiple accounts,
username have restricted word like "test_user",
so if single account username contains "test_user",
username is restricted.

hope transaction solve problem of same id, or maybe i am wrong?
$this->db->trans_start();
.
.
$this->db->trans_complete();

thank you for help
#6

[eluser]smdelektronik[/eluser]
that i use to create multiple accounts,
username have restricted word like "test_user",
so if single account username contains "test_user",
username is restricted.

hope transaction solve problem of same id, or maybe i am wrong?

$this->db->trans_start();
.
//my code
.
$this->db->trans_complete();

thank you for help




Theme © iAndrew 2016 - Forum software by © MyBB