CodeIgniter Forums
[Solved] When Insert New User Get 2 preg_match() errors - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [Solved] When Insert New User Get 2 preg_match() errors (/thread-60571.html)



[Solved] When Insert New User Get 2 preg_match() errors - El Forum - 04-28-2014

[eluser]riwakawd[/eluser]
I don't know why I am getting 2 errors some array issue which I am not sure of does not give the error on register model on codeigniter?

Error 1
A PHP Error was encountered
Severity: Warning
Message: preg_match() expects parameter 2 to be string, array given
Filename: mysql/mysql_driver.php
Line Number: 199

Error 2
A PHP Error was encountered
Severity: Warning
Message: preg_match() expects parameter 2 to be string, array given
Filename: database/DB_driver.php
Line Number: 622


Code:
public function insert_user() {

    $sql = array(
     'firstname' => $this->input->post('firstname'),
     'ip' => $this->session->userdata('ip_address'),
     'lastname' => $this->input->post('lastname'),
     'email' => $this->input->post('email'),
     'country' => $this->input->post('country'),
     'username' => $this->input->post('username'),
     'password' => $this->input->post('password')
  );

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

  $result  = $this->db->query($sql);

  if ($this->db->affected_rows() === 1) {
   return 'firstname';
  } else {
   $this->load->library('email');
   $this->email->from('[email protected]', "From Website Admin");
   $this->email->to('[email protected]');
   $this->email->subject('Problem Inserting User Into Database');

   if(isset($email)) {
    $this->email->message('Unable To Register And Insert User With Email Of' .$email . 'To The Database');
   } else {
    $this->email->message('Unable To Register And Insert User To The Database');
   }

   $this->email->send();
   return false;
  }

   }



[Solved] When Insert New User Get 2 preg_match() errors - El Forum - 04-28-2014

[eluser]riwakawd[/eluser]
Please note: I worked it out I had to make some changes. Here below is what have changed.

Now just have to find away to make new user have own user id.

Code:
public function insert_user() {

    $this->db->where('username', $this->input->post('username'));
  $query  = $this->db->get('users');

  if ($query->num_rows > 0) {
   echo '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>';
   echo "Username already taken";
   echo '</strong></div>';
  } else {

   $this->load->library('bcrypt');

   $sql = array(
      'firstname' => $this->input->post('firstname'),
      'ip' => $this->session->userdata('ip_address'),
      'lastname' => $this->input->post('lastname'),
      'email' => $this->input->post('email'),
      'country' => $this->input->post('country'),
      'username' => $this->input->post('username'),
    
   );

   $result = $this->db->insert('users', $sql);
   return $result;
  }

   }



[Solved] When Insert New User Get 2 preg_match() errors - El Forum - 04-28-2014

[eluser]Tim Brownlaw[/eluser]
Hi,

When you perform an insert, using $this->db->insert_id() will return the value of the newly created primary key.
Which is normally an auto incrementing integer...

So if you use an id as your primary key as your members id, that should work out for you.

cheers
Tim


[Solved] When Insert New User Get 2 preg_match() errors - El Forum - 04-28-2014

[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1398753608"]Hi,

When you perform an insert, using $this->db->insert_id() will return the value of the newly created primary key.
Which is normally an auto incrementing integer...

So if you use an id as your primary key as your members id, that should work out for you.

cheers
Tim[/quote]

I have on my sql both user and id as int. but trying now to get the user id have the same as id. Are you saying to put AUTO_INCREMENT every thing else is working but not the user_id just trying to get that the same when user registers. If you know what I mean.


Code:
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `ip` varchar(45) NOT NULL,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `joined_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;



[Solved] When Insert New User Get 2 preg_match() errors - El Forum - 04-29-2014

[eluser]Tim Brownlaw[/eluser]
Well in this...
Code:
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,

The id is your unique id which would be your user id... There isn't any point in having both...

So you could loose the id and rename it to user_id
Code:
`user_id` int(11) NOT NULL AUTO_INCREMENT,

or just have id as your user id

Code:
`id` int(11) NOT NULL AUTO_INCREMENT,

you'll be using the user_id as a foreign key to any other tables so if you like to have them all the same name across tables use user_id as your primary key

Code:
CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(45) NOT NULL,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `joined_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

So you don't need to have an id and a user_id... Choose one or the other!

Cheers
Tim


[Solved] When Insert New User Get 2 preg_match() errors - El Forum - 04-29-2014

[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1398755351"]Well in this...
Code:
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,

The id is your unique id which would be your user id... There isn't any point in having both...

So you could loose the id and rename it to user_id
Code:
`user_id` int(11) NOT NULL AUTO_INCREMENT,

or just have id as your user id

Code:
`id` int(11) NOT NULL AUTO_INCREMENT,

you'll be using the user_id as a foreign key to any other tables so if you like to have them all the same name across tables use user_id as your primary key

Code:
CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(45) NOT NULL,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `joined_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

So you don't need to have an id and a user_id... Choose one or the other!

Cheers
Tim[/quote]

Ok Will do it that way.


[Solved] When Insert New User Get 2 preg_match() errors - El Forum - 04-29-2014

[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1398755351"]Well in this...
Code:
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,

The id is your unique id which would be your user id... There isn't any point in having both...

So you could loose the id and rename it to user_id
Code:
`user_id` int(11) NOT NULL AUTO_INCREMENT,

or just have id as your user id

Code:
`id` int(11) NOT NULL AUTO_INCREMENT,

you'll be using the user_id as a foreign key to any other tables so if you like to have them all the same name across tables use user_id as your primary key

Code:
CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(45) NOT NULL,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `country` varchar(255) NOT NULL,
  `joined_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

So you don't need to have an id and a user_id... Choose one or the other!

Cheers
Tim[/quote]

I have it all working now register side of things done cheers all for help. Finally also got date helper worked out.

Talk to you soon,


[Solved] When Insert New User Get 2 preg_match() errors - El Forum - 04-29-2014

[eluser]InsiteFX[/eluser]
The only time you need other id's is if your referencing another table.